When I my function, my hash is empty. Why?
Here is my code:
#include <stdio.h>
#include <string.h>
#include "uthash.h"
struct oid_struct {
char descr[20];
char oid[50];
UT_hash_handle hh;
};
testadd( struct oid_struct* oid_hash){
struct oid_struct *element;
element=(struct oid_struct*) malloc(sizeof(struct oid_struct));
strcpy(element->descr, "foo");
strcpy(element->oid, "1.2.1.34");
HASH_ADD_STR(oid_hash, descr, element);
printf("Hash has %d entries\n",HASH_COUNT(oid_hash));
}
main(){
struct oid_struct *oid_hash = NULL, *lookup;
testadd(oid_hash);
printf("Hash has %d entries\n",HASH_COUNT(oid_hash));
}
Here is the output:
# gcc hashtest.c
# ./a.out
Hash has 1 entries
Hash has 0 entries
#
C passes arguments by value, meaning a copy of
oid_hashis being changed insidetestadd()so the change is invisible to the caller. Pass the address ofoid_hashtotestadd():Note the casting of return value of
malloc()is not required.