7b-struct-int-array.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2017,2018 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. struct foo
  22. {
  23. int a;
  24. int b;
  25. };
  26. struct foo g_foo[2] = { 0, 1, 2, 3 };
  27. struct bar
  28. {
  29. int bar[4];
  30. };
  31. struct bar g_bar = { 101, 102 };
  32. typedef struct bar bar_struct;
  33. typedef struct bar foo_struct;
  34. int
  35. main ()
  36. {
  37. if (g_foo[0].a != 0)
  38. return 1;
  39. if (g_foo[0].b != 1)
  40. return 2;
  41. if (g_foo[1].a != 2)
  42. return 3;
  43. if (g_foo[1].b != 3)
  44. return 4;
  45. void *p = &g_foo;
  46. struct foo *pfoo = (((struct foo *) p) + 1);
  47. if (pfoo->a != 2)
  48. return 5;
  49. if (pfoo->b != 3)
  50. return 6;
  51. int *pi = &g_foo;
  52. if (*pi != 0)
  53. return 7;
  54. pi = &g_bar;
  55. if (*pi != 101)
  56. return 8;
  57. struct bar bar = { 0x22, 0x33 };
  58. pi = &bar;
  59. if (*pi != 0x22)
  60. return 9;
  61. bar_struct bs;
  62. bs.bar[0] = 102;
  63. pi = &bs;
  64. if (*pi != 102)
  65. return 10;
  66. foo_struct fs;
  67. fs.bar[0] = 0x22;
  68. fs.bar[1] = 0x33;
  69. pi = &fs;
  70. if (*pi != 0x22)
  71. return 11;
  72. pi++;
  73. if (*pi != 0x33)
  74. return 12;
  75. foo_struct *pfs = &fs;
  76. pfs->bar[3] = 0x44;
  77. pfs->bar[4] = 0x55;
  78. pi = &fs.bar[3];
  79. if (*pi != 0x44)
  80. return 13;
  81. pi++;
  82. if (*pi != 0x55)
  83. return 14;
  84. return 0;
  85. }