typedef struct {
char ID[10]; /* key */
char name[10];
int age;
UT_hash_handle hh; /* makes this structure hashable */
}HashTable;
Declaration: HashTable *g_table = NULL Remember to initialize to NULL and preferably be global variable. Otherwise, better to pass a pointer to the hash pointer (the hash macros modify it)
/* bad, but works with Hash as global variable */
void add_user(struct my_struct *hash, int user_id, char *name) {
...
HASH_ADD_INT(hash, id, s);
}
/* good */
void add_user(struct my_struct **pHash, int user_id, char *name) { ...
...
HASH_ADD_INT(*pHash, id, s);
}
Add The second parameter is the name of the key field
Convenience/General macros: make UT_hash_handle named hh to use convenience macros, support integer, pointer or string keys, otherwise name UT_hash_handle except hh!