1 #include "uthash.h"
2 #include <stdio.h>
3 #include <stdlib.h>  /* malloc */
4 
5 typedef struct person_t {
6     char first_name[10];
7     int id;
8     UT_hash_handle hh;
9 } person_t;
10 
main(int argc,char * argv[])11 int main(int argc, char*argv[])
12 {
13     person_t *people=NULL, *person;
14     const char **name;
15     const char * names[] = { "bob", "jack", "gary", "ty", "bo", "phil", "art",
16                              "gil", "buck", "ted", NULL
17                            };
18     int id=0;
19 
20     for(name=names; *name != NULL; name++) {
21         person = (person_t*)malloc(sizeof(person_t));
22         if (person == NULL) {
23             exit(-1);
24         }
25         strcpy(person->first_name, *name);
26         person->id = id++;
27         HASH_ADD_STR(people,first_name,person);
28         printf("added %s (id %d)\n", person->first_name, person->id);
29     }
30 
31     for(name=names; *name != NULL; name++) {
32         HASH_FIND_STR(people,*name,person);
33         if (person != NULL) {
34             printf("found %s (id %d)\n", person->first_name, person->id);
35         } else {
36             printf("failed to find %s\n", *name);
37         }
38     }
39     return 0;
40 }
41