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