diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2020-12-01 13:33:34 +0100 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2020-12-01 13:33:34 +0100 |
commit | c8bf38e5fb717d40635a2a89b22ed71b0de4266b (patch) | |
tree | 63751b2f5497c6f99e1c6a78f23a8e6e5c49833f /tests/test15.c |
Squashed 'dependencies/uthash/' content from commit 8e67ced
git-subtree-dir: dependencies/uthash
git-subtree-split: 8e67ced1d1c5bd8141c542a22630e6de78aa6b90
Diffstat (limited to 'tests/test15.c')
-rw-r--r-- | tests/test15.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/tests/test15.c b/tests/test15.c new file mode 100644 index 000000000..56140928d --- /dev/null +++ b/tests/test15.c @@ -0,0 +1,40 @@ +#include <string.h> /* strcpy */ +#include <stdlib.h> /* malloc */ +#include <stdio.h> /* printf */ +#include "uthash.h" + +struct my_struct { + char name[10]; /* 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); + } + strcpy(s->name, *n); + s->id = i++; + HASH_ADD_STR( users, 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; +} |