#include <stdio.h>
struct a {
int a, b, c;
};
struct b {
double a, b, c, d;
};
struct c {
char s[100];
};
struct d {
int a, b, c;
double n[10];
};
union x {
struct a a;
struct b b;
struct c c;
struct d d;
};
typedef struct {
enum { A, B, C, D } type;
union x value;
} Obj;
int main(void)
{
Obj objects[100], *pobj;
int n;
int i;
objects[0].type = A;
objects[0].value.a.a = 1;
objects[0].value.a.b = 2;
objects[0].value.a.c = 3;
objects[1].type = B;
objects[1].value.b.a = 1.5;
objects[1].value.b.b = 2.5;
objects[1].value.b.c = 3.5;
objects[1].value.b.d = 4.5;
objects[2].type = C;
sprintf(objects[2].value.c.s, "abc");
objects[3].type = A;
objects[3].value.a.a = 4;
objects[3].value.a.b = 5;
objects[3].value.a.c = 6;
n = 4;
for (i = 0; i < n; i++) {
pobj = objects + i;
switch (pobj->type) {
case A:
printf("A: %d %d %d\n",
pobj->value.a.a,
pobj->value.a.b,
pobj->value.a.c);
break;
case B:
printf("B: %g %g %g %g\n",
pobj->value.b.a,
pobj->value.b.b,
pobj->value.b.c,
pobj->value.b.d);
break;
case C:
printf("C: %s\n", pobj->value.c.s);
break;
case D:
printf("D: %g\n", pobj->value.d.n[0]);
break;
}
}
return 0;
}