nested_struct.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of M2-Planet.
  3. *
  4. * M2-Planet is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * M2-Planet is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include<stdlib.h>
  18. #include<stdio.h>
  19. struct bar
  20. {
  21. char z;
  22. int x;
  23. int y;
  24. };
  25. struct foo
  26. {
  27. struct foo* next;
  28. struct foo* prev;
  29. struct bar* a;
  30. };
  31. void print_hex(int a, int count)
  32. {
  33. if(count <= 0) return;
  34. print_hex(a >> 4, count - 1);
  35. putchar((a & 15) + 48);
  36. }
  37. int main()
  38. {
  39. struct foo* a = malloc(sizeof(struct foo));
  40. struct foo* b = malloc(sizeof(struct foo));
  41. struct bar* c = malloc(sizeof(struct bar));
  42. struct bar* d = malloc(sizeof(struct bar));
  43. c->x = 0x35419896;
  44. c->y = 0x57891634;
  45. d->x = 0x13579246;
  46. d->y = 0x64297531;
  47. a->a = c;
  48. b->a = d;
  49. a->next = b;
  50. a->prev = b;
  51. b->next = a;
  52. b->prev = a;
  53. print_hex(a->next->next->a->x, 8);
  54. print_hex(b->prev->prev->a->y, 8);
  55. print_hex(b->next->a->x, 8);
  56. print_hex(b->prev->a->y, 8);
  57. putchar(10);
  58. return sizeof(struct foo);
  59. }