7l-struct-any-size-array.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2017 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. #if __MESC__
  22. #define __attribute__(x)
  23. #endif
  24. struct foo13
  25. {
  26. int a;
  27. int b;
  28. int c;
  29. char d;
  30. } __attribute__ ((packed));
  31. ;
  32. struct foo16
  33. {
  34. int a;
  35. int b;
  36. int c;
  37. int d;
  38. };
  39. struct foo13 tab14[3];
  40. struct foo16 tab16[3];
  41. int
  42. main ()
  43. {
  44. unsigned char *p;
  45. tab14[1].a = -1;
  46. tab14[1].b = -1;
  47. tab14[1].c = -1;
  48. tab14[1].d = -1;
  49. p = &tab14;
  50. for (int i = 0; i < sizeof (struct foo13) * 2; i++)
  51. {
  52. if (i < 10)
  53. eputs (" ");
  54. eputs (itoa (i));
  55. eputs (": ");
  56. eputs (itoa (p[i]));
  57. eputs ("\n");
  58. }
  59. for (int i = 0; i < sizeof (struct foo13); i++)
  60. if (p[i] != 0)
  61. return 1 + i;
  62. for (int i = sizeof (struct foo13); i < 2 * sizeof (struct foo13); i++)
  63. if (p[i] != 255)
  64. return 1 + i;
  65. tab16[1].a = -1;
  66. tab16[1].b = -1;
  67. tab16[1].c = -1;
  68. tab16[1].d = -1;
  69. p = &tab16;
  70. for (int i = 0; i < sizeof (struct foo16) * 2; i++)
  71. {
  72. if (i < 10)
  73. eputs (" ");
  74. eputs (itoa (i));
  75. eputs (": ");
  76. eputs (itoa (p[i]));
  77. eputs ("\n");
  78. }
  79. for (int i = 0; i < sizeof (struct foo16); i++)
  80. if (p[i] != 0)
  81. return 1 + i;
  82. for (int i = sizeof (struct foo16); i < 2 * sizeof (struct foo16); i++)
  83. if (p[i] != 255)
  84. return 1 + i;
  85. return 0;
  86. }