If I have these structures:
typedef struct { int x; } foo;
typedef struct { foo f; } bar;
Normally you would access x
through
In C++, it is possible in two ways. The first is to use inheritence. The second is for bar
to contain a reference member named x
(int &x
), and constructors that initialise x
to refer to f.x
.
In C, it is not possible.
In C (99 and onward) you can access the common initial sub-sequence of union members, even if they weren't the last member written to1.
In C11, you can have anonymous union members. So:
typedef struct { int x; } foo;
typedef struct {
union {
foo f;
int x;
};
} bar;