summaryrefslogtreecommitdiff
path: root/test/regression/compound.c
blob: 7c6bd2a756a087b97508abdcd3aac29d04ca7d82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* Testing compound literals */

#include <stdio.h>

struct point { int x, y; };
struct line { struct point from, to; };

void printpoint (struct point p)
{
  printf("{x = %d, y = %d}\n", p.x, p.y);
}

void printpointref (struct point * p)
{
  printf("{x = %d, y = %d}\n", p->x, p->y);
}

void printline (struct line l)
{
  printf("{from = {x = %d, y = %d}, to = {x = %d, y = %d}\n",
         l.from.x, l.from.y, l.to.x, l.to.y);
}

static inline struct point diff(struct point a, struct point b)
{
  return (struct point){ b.x - a.x, b.y - a.y };
}

/* H&S section 7.4.5 */

char * temp1 = (char []) {"/temp/XXXXXX"};
char * temp2 = "/temp/XXXXXX";

int pow2(int n)
{
  if (n >= 0 && n <= 7)
    return (const int []) {1,2,4,8,16,32,64,128} [n];
  else
    return -1;
}

void test1(int n)
{
  printf("temp1 = \"%s\"\n", temp1);
  printf("temp2 = \"%s\"\n", temp2);
  temp1[6] = '!';
  printf("mutated temp1 = \"%s\"\n", temp1);

  printpoint((struct point){.x=12, .y=n+3});
  printpointref(&(struct point){n,-n});
  printline((struct line){n,n+1,n+2,n+3});
  printline((struct line){.from = (struct point){n-3,n-2},
                          .to   = (struct point){n-1,n}});
  printpoint(diff((struct point){n,n}, (struct point){1,1}));
  int * ptrs[5];
  int i = 0;
 again:
  ptrs[i] = (int [1]){i};
  if (++i < 5) goto again;
  printf("ptrs contains %d %d %d %d %d\n",
         *(ptrs[0]), *(ptrs[1]), *(ptrs[2]), *(ptrs[3]),*(ptrs[4]));
  i = 0;
  ptrs[0] = (int [1]){i++};
  ptrs[1] = (int [1]){i++};
  ptrs[2] = (int [1]){i++};
  ptrs[3] = (int [1]){i++};
  ptrs[4] = (int [1]){i++};
  printf("ptrs contains %d %d %d %d %d\n",
         *(ptrs[0]), *(ptrs[1]), *(ptrs[2]), *(ptrs[3]),*(ptrs[4]));
}

/* Examples from GCC's manual */

struct foo { int a; char b[2]; } structure;

char **foo = (char *[]) { "x", "y", "z" };

static struct foo x = (struct foo) {1, 'a', 'b'};
// Dubious examples: GCC refuses them, Clang warns.
// static int y[] = (int []) {1, 2, 3};
// static int z[] = (int [3]) {1};

void test2(int n)
{
  structure = (struct foo) {n, 'a', 0};
  printf("structure = {a = %d, b = \"%s\"}\n", structure.a, structure.b);
  printf("foo = { \"%s\", \"%s\", \"%s\" }\n", foo[0], foo[1], foo[2]);
  printf("x = {a = %d, b[0] = '%c', b[1] = '%c'}\n", x.a, x.b[0], x.b[1]);
}

/* Example gathered from various places */

union U { float f; int i; };

void printU(int kind, const union U u)
{
  switch (kind) {
  case 0:  printf("{f = %f}\n", u.f); break;
  case 1:  printf("{i = %d}\n", u.i); break;
  }
}

struct list { char * value; struct list * next; };

void printlist(struct list * l)
{
  for (; l != NULL; l = l->next) printf("\"%s\", ", l->value);
  printf("NULL\n");
}

void printintref(int * p)
{
  printf("%d\n", *p);
}

struct S { int n; int *p; };

void printS(struct S s)
{
  printf("{ n = %d, p -> {%d,%d,%d,%d} }\n",
         s.n, s.p[0], s.p[1], s.p[2], s.p[3]);
}

void test3(void)
{
  printU(0, (const union U){0.25});
  printU(1, (const union U){.i = 11});
  printf("1 + 3 = %d\n", (int){1} + (int){3});
  for (int i = 0; i < 3; i++) printpoint((struct point){i,i});
  printpoint((struct point){1});
  printpoint((struct point){.y=2});
  printlist(&((struct list){"first", &((struct list){"second", NULL})}));
  printintref(&((int){77}));
  struct S s = (struct S) {3, (int[4]){0,1,2}};
  printS(s);
  printS((struct S) {4, (int[]){0,1,2,3}});
}

int main(void)
{
  test1(42);
  test2(12);
  test3();
  return 0;
}