7e-struct-array-access.c 2.0 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. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. struct symbol
  25. {
  26. int len;
  27. char str[10];
  28. //int len;
  29. };
  30. struct symbol *list[2];
  31. struct symbol s0;
  32. struct symbol s1;
  33. struct symbol **plist;
  34. char *
  35. find0 ()
  36. {
  37. strcpy (s0.str, "foo");
  38. strcpy (s1.str, "bar");
  39. list[0] = &s0;
  40. list[1] = &s1;
  41. //return s0.str;
  42. //struct symbol *s = &s0;
  43. struct symbol *s = list[0];
  44. return s->str;
  45. }
  46. char *
  47. find1 ()
  48. {
  49. return list[1]->str;
  50. }
  51. char *
  52. find2 ()
  53. {
  54. plist = malloc (8);
  55. struct symbol *p0 = malloc (sizeof (struct symbol));
  56. struct symbol *p1 = malloc (sizeof (struct symbol));
  57. strcpy (p0->str, "pfoo");
  58. strcpy (p1->str, "pbar");
  59. plist[0] = p0;
  60. plist[1] = p1;
  61. int i = 3;
  62. return plist[i - 2]->str;
  63. }
  64. int
  65. main ()
  66. {
  67. char *s = find0 ();
  68. eputs (s);
  69. eputs ("\n");
  70. if (strcmp (s, "foo"))
  71. return 1;
  72. if (strcmp (list[0]->str, "foo"))
  73. return 2;
  74. s = find1 ();
  75. eputs (s);
  76. eputs ("\n");
  77. if (strcmp (s, "bar"))
  78. return 3;
  79. if (strcmp (list[1]->str, "bar"))
  80. return 4;
  81. s = find2 ();
  82. eputs (s);
  83. eputs ("\n");
  84. if (strcmp (s, "pbar"))
  85. return 5;
  86. list[1]->len = 2;
  87. if (list[1]->len != 2)
  88. return 6;
  89. return 0;
  90. }