7f-struct-pointer-arithmetic.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include <mes/lib.h>
  22. #include <stdio.h>
  23. struct foo;
  24. typedef struct foo foo_struct;
  25. struct foo
  26. {
  27. //struct foo **foo;
  28. foo_struct **foo;
  29. };
  30. struct foo g_foo[2];
  31. int
  32. main ()
  33. {
  34. struct foo foo;
  35. foo.foo = g_foo;
  36. void *p;
  37. void *q;
  38. p = &foo.foo[0];
  39. q = foo.foo;
  40. eputs ("f:");
  41. eputs (itoa (foo.foo));
  42. eputs ("\n");
  43. eputs ("p:");
  44. eputs (itoa (p));
  45. eputs ("\n");
  46. eputs ("q:");
  47. eputs (itoa (q));
  48. eputs ("\n");
  49. if (q != p)
  50. return 1;
  51. p = &foo.foo[1];
  52. q = foo.foo + 1;
  53. eputs ("f:");
  54. eputs (itoa (foo.foo));
  55. eputs ("\n");
  56. eputs ("p:");
  57. eputs (itoa (p));
  58. eputs ("\n");
  59. eputs ("q:");
  60. eputs (itoa (q));
  61. eputs ("\n");
  62. if (q != p)
  63. return 2;
  64. struct foo *pfoo = &foo;
  65. p = &pfoo->foo[1];
  66. q = pfoo->foo + 1;
  67. eputs ("f:");
  68. eputs (itoa (pfoo->foo));
  69. eputs ("\n");
  70. eputs ("p:");
  71. eputs (itoa (p));
  72. eputs ("\n");
  73. eputs ("q:");
  74. eputs (itoa (q));
  75. eputs ("\n");
  76. if (q != p)
  77. return 3;
  78. return 0;
  79. }