125-anon-union.c (1139B)
1 /* C11 §6.7.2.1 — members of an anonymous struct/union are addressed 2 * as if they were members of the enclosing aggregate. tcc.c's 3 * `struct Sym` relies on this: it has three back-to-back anonymous 4 * unions and code accesses `s->r`, `s->c`, `s->d`, etc. directly. */ 5 6 struct S { 7 int v; 8 union { 9 int a; 10 long b; 11 }; 12 union { 13 long c; 14 int *d; 15 }; 16 int trailing; 17 }; 18 19 int main(int argc, char **argv) { 20 struct S s = { .v = 1, .b = 0, .c = 100, .trailing = 99 }; 21 s.a = 7; /* into anon union 1 (offset 8) */ 22 23 if (s.v != 1) return 1; 24 if (s.a != 7) return 2; 25 if (s.b != 7) return 3; /* a and b alias */ 26 if (s.c != 100) return 4; 27 if (s.trailing != 99) return 5; 28 29 /* Pointer-form access through the anon union. */ 30 struct S *p = &s; 31 p->c = 200; 32 if (s.c != 200) return 6; 33 34 /* Designated init through the anonymous member. */ 35 struct S t = { .v = 9, .a = 10, .c = 11, .trailing = 12 }; 36 if (t.v != 9) return 7; 37 if (t.a != 10) return 8; 38 if (t.c != 11) return 9; 39 if (t.trailing != 12) return 10; 40 41 return 0; 42 }