Research portable Memory game | Исследовать портируемую игру Память
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
935B

  1. #include <string.h> /* strcpy */
  2. #include <stdlib.h> /* malloc */
  3. #include <stdio.h> /* printf */
  4. #include "uthash.h"
  5. struct my_struct {
  6. const char *name; /* key */
  7. int id;
  8. UT_hash_handle hh; /* makes this structure hashable */
  9. };
  10. int main()
  11. {
  12. const char **n, *names[] = { "joe", "bob", "betty", NULL };
  13. struct my_struct *s, *tmp, *users = NULL;
  14. int i=0;
  15. for (n = names; *n != NULL; n++) {
  16. s = (struct my_struct*)malloc(sizeof(struct my_struct));
  17. if (s == NULL) {
  18. exit(-1);
  19. }
  20. s->name = *n;
  21. s->id = i++;
  22. HASH_ADD_KEYPTR( hh, users, s->name, strlen(s->name), s );
  23. }
  24. HASH_FIND_STR( users, "betty", s);
  25. if (s != NULL) {
  26. printf("betty's id is %d\n", s->id);
  27. }
  28. /* free the hash table contents */
  29. HASH_ITER(hh, users, s, tmp) {
  30. HASH_DEL(users, s);
  31. free(s);
  32. }
  33. return 0;
  34. }