72-typedef-struct-def.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2017,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. *
  5. * This file is part of GNU Mes.
  6. *
  7. * GNU Mes is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * GNU Mes is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <mes/lib.h>
  21. #include <stdio.h>
  22. typedef struct foo
  23. {
  24. int i;
  25. } foo;
  26. typedef struct
  27. {
  28. int i;
  29. struct foo f;
  30. struct foo *p;
  31. } bar;
  32. bar baz[2] = { 1, 2, 3, 4, 5, 6 };
  33. bar *list[2];
  34. //NYACC
  35. //#define offsetof(type, field) ((size_t) &((type *)0)->field)
  36. #if __MESC__
  37. #define offsetof(type, field) (&((type *)0)->field)
  38. #else
  39. #define offsetof(type, field) ((size_t)&((type *)0)->field)
  40. #endif
  41. int
  42. main ()
  43. {
  44. foo f = { 1 };
  45. printf ("f.i=%d\n", f.i);
  46. bar b = { 1, 2, &f };
  47. printf ("b.i=%d\n", b.i);
  48. printf ("b.f.i=%d\n", b.f.i);
  49. if (b.f.i != 2)
  50. return 1;
  51. printf ("b.p->i=%d\n", b.p->i);
  52. if (b.p->i != 1)
  53. return 2;
  54. bar *p = &b;
  55. p->i = 2;
  56. printf ("p->i=%d\n", b.i);
  57. p->i++;
  58. printf ("p->i=%d\n", b.i);
  59. p->i--;
  60. printf ("p->i=%d\n", b.i);
  61. printf ("p->f.i=%d\n", p->f.i);
  62. if (p->f.i != 2)
  63. return 3;
  64. printf ("p->p->i=%d\n", p->p->i);
  65. if (p->p->i != 1)
  66. return 4;
  67. bar **pp = &p;
  68. (*pp)->i = 3;
  69. printf ("(*pp)->i=%d\n", b.i);
  70. printf ("sizeof i:%d\n", sizeof (p->i));
  71. if ((sizeof p->i) != 4)
  72. return 5;
  73. printf ("offsetof g=%d\n", (offsetof (bar, f)));
  74. #if __MESC__
  75. //if ((offsetof (bar ,f)) != 4) return 6;
  76. //#define offsetof(type, field) (&((type *)0)->field)
  77. if ((&((bar *) 0)->f) != 4)
  78. return 6;
  79. #else
  80. if ((offsetof (bar, f)) != 4)
  81. return 6;
  82. #endif
  83. printf ("(*pp)->b.i=%d\n", (*pp)->f.i);
  84. if ((*pp)->f.i != 2)
  85. return 7;
  86. if (baz[0].i != 1)
  87. return 8;
  88. printf ("baz[0].f.i=%d\n", baz[0].f.i);
  89. if (baz[0].f.i != 2)
  90. return 9;
  91. printf ("baz[1].i=%d\n", baz[1].i);
  92. if (baz[1].i != 4)
  93. return 10;
  94. printf ("baz[1].f.i=%d\n", baz[1].f.i);
  95. if (baz[1].f.i != 5)
  96. return 11;
  97. return 0;
  98. }