1 // struct definition and declaration
2 struct a {
3 int a;
4 int b;
5 } c;
6
7 // Useful anonymous struct declaration
8 struct {
9 int y;
10 } anon1, anon2;
11
12 // forward declarations
13 struct a;
14 struct b;
15 struct c;
16
17 struct b {int a; int b; };
18
19 // struct c {b g; }; // syntax error.
20
21 // struct s {float c,a,b,c;} s; // duplicate struct member
22
23 struct c {struct b g; };
24
25 // struct a { int w; }; // error
26
testCopying()27 void testCopying() {
28 struct a {int a[10]; char c;} a, b;
29 a.c = 37;
30 b.c = 38;
31 b = a;
32 printf("testCopying: %d == %d\n", a.c, b.c);
33 }
34
testUnion()35 void testUnion() {
36 union u;
37 union u {float f;int i;} u;
38 u.f = 1.0f;
39 printf("testUnion: %g == 0x%08x\n", u.f, u.i);
40 }
41
42 struct v {float x, y, z, w; };
43
add(struct v * result,struct v * a,struct v * b)44 void add(struct v* result, struct v* a, struct v* b) {
45 result->x = a->x + b->x;
46 result->y = a->y + b->y;
47 result->z = a->z + b->z;
48 result->w = a->w + b->w;
49 }
50
set(struct v * v,float x,float y,float z,float w)51 void set(struct v* v, float x, float y, float z, float w) {
52 v->x = x;
53 v->y = y;
54 v->z = z;
55 v->w = w;
56 }
57
print(struct v * v)58 void print(struct v* v) {
59 printf("(%g, %g, %g, %g)\n", v->x, v->y, v->z, v->w);
60 }
61
testArgs()62 void testArgs() {
63 struct v a, b, c;
64 set(&a, 1.0f, 2.0f, 3.0f, 4.0f);
65 set(&b, 5.0f, 6.0f, 7.0f, 8.0f);
66 add(&c, &a, &b);
67 printf("testArgs: ");
68 print(&c);
69 }
70
main()71 int main() {
72 anon1.y = 3;
73 anon2.y = anon1.y;
74
75 testCopying();
76 testUnion();
77 testArgs();
78
79 struct c cc;
80 cc.g.a = 3;
81 c.a = 1;
82 c.b = 3;
83 struct a {int x, y; } z;
84 // struct a {int x, y; } z2;
85 z.x = c.a;
86 struct a *pA;
87 pA = &z;
88 pA->x += 5;
89 return pA->x;
90 }
91