
help: unnamed unions within structures
Quote:
> Hi! I've recently encountered some code with the following structure
> declaration:
> typedef struct mystruct {
> int a;
> union {
> char b[20];
> struct {
> int c;
> int d;
> } e;
> };
> } mystruct;
> the above was used in the following manner:
> f()
> {
> int Off;
> .
> .
> Off = offsetof(mystruct, e.c);
> .
> .
> }
> i tried compiling using gcc but it terminates with an error indicating that
> e is not a member of mystruct.
> has anyone ever successfully used this kind of structure declaration?
> barry
<Jack>
That's very good of gcc for a change. The C standard does not allow anonymous
unions anywhere. C++ allows them in some circumstances, but I don't know if
this is one of them. Even in C++ I am not sure how you would access members
of the union, because just like the message says, e is not a member of
mystruct.
e is a member of the union and the union is a member of mystruct.
Was this C++ code to begin with? Can you just give the union a name?
</Jack>