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.

58 lines
1.2KB

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "uthash.h"
  5. /* hash of hashes */
  6. typedef struct item {
  7. char name[10];
  8. struct item *sub;
  9. int val;
  10. UT_hash_handle hh;
  11. } item_t;
  12. int main()
  13. {
  14. item_t *item1, *item2, *tmp1, *tmp2;
  15. item_t *items=NULL;
  16. /* make initial element */
  17. item_t *i = (item_t *)malloc(sizeof(*i));
  18. if (i == NULL) {
  19. exit(-1);
  20. }
  21. strcpy(i->name, "bob");
  22. i->sub = NULL;
  23. i->val = 0;
  24. HASH_ADD_STR(items, name, i);
  25. /* add a sub hash table off this element */
  26. item_t *s = (item_t *)malloc(sizeof(*s));
  27. if (s == NULL) {
  28. exit(-1);
  29. }
  30. strcpy(s->name, "age");
  31. s->sub = NULL;
  32. s->val = 37;
  33. HASH_ADD_STR(i->sub, name, s);
  34. /* iterate over hash elements */
  35. HASH_ITER(hh, items, item1, tmp1) {
  36. HASH_ITER(hh, item1->sub, item2, tmp2) {
  37. printf("$items{%s}{%s} = %d\n", item1->name, item2->name, item2->val);
  38. }
  39. }
  40. /* clean up both hash tables */
  41. HASH_ITER(hh, items, item1, tmp1) {
  42. HASH_ITER(hh, item1->sub, item2, tmp2) {
  43. HASH_DEL(item1->sub, item2);
  44. free(item2);
  45. }
  46. HASH_DEL(items, item1);
  47. free(item1);
  48. }
  49. return 0;
  50. }