diff options
Diffstat (limited to 'tests/test40.c')
-rw-r--r-- | tests/test40.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/tests/test40.c b/tests/test40.c new file mode 100644 index 000000000..af754611c --- /dev/null +++ b/tests/test40.c @@ -0,0 +1,40 @@ +#include <string.h> /* strcpy */ +#include <stdlib.h> /* malloc */ +#include <stdio.h> /* printf */ +#include "uthash.h" + +struct my_struct { + const char *name; /* key */ + int id; + UT_hash_handle hh; /* makes this structure hashable */ +}; + + +int main() +{ + const char **n, *names[] = { "joe", "bob", "betty", NULL }; + struct my_struct *s, *tmp, *users = NULL; + int i=0; + + for (n = names; *n != NULL; n++) { + s = (struct my_struct*)malloc(sizeof(struct my_struct)); + if (s == NULL) { + exit(-1); + } + s->name = *n; + s->id = i++; + HASH_ADD_KEYPTR( hh, users, s->name, strlen(s->name), s ); + } + + HASH_FIND_STR( users, "betty", s); + if (s != NULL) { + printf("betty's id is %d\n", s->id); + } + + /* free the hash table contents */ + HASH_ITER(hh, users, s, tmp) { + HASH_DEL(users, s); + free(s); + } + return 0; +} |