По стандарту из union можно писать только то поле, которое было в него записано последним. А если речь идет о структурах?
struct type_a {
int field1;
char field2;
};
struct type_b {
struct type_a a;
double field3;
};
void do_a(struct type_a *a);
void do_b(struct type_b *b);
typedef union {
struct type_a a;
struct type_b b;
} super_type;
/* x->b гарантированно был инициализирован */
void do_c(super_type *x) {
do_a(&x->a);
do_b(&x->b);
}
Или вообще вот так (при условии что безымянная структура внутри union-а всегда совпадает с S):
struct S {
char field1;
int field2;
};
void do_S(struct S *s);
struct M {
double x, y;
union {
struct {
char field1;
int field2;
};
struct S s;
};
};
void do_M(struct M *m)
{
m->field1 = '5';
m->field2 = 5;
do_S(&m->s);
}