How to compile C code with anonymous structs / unions?

according to http://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html#Unnamed-Fields

-fms-extensions will enable the feature you (and I) want.


(This answer applies to C99, not C11).

C99 does not have anonymous structures or unions. You have to name them:

typedef struct {
    union {
        struct {
            float x, y, z;
        } individual;
        float xyz[3];
    } data;
} Vector3;

And then you have to use the name when accessing them:

assert(&v.data.xyz[0] == &v.data.individual.x);

In this case, because your top level structure has a single item of type union, you could simplify this:

typedef union {
    struct {
        float x, y, z;
    } individual;
    float xyz[3];
} Vector3;

and accessing the data now becomes:

assert(&v.xyz[0] == &v.individual.x);

The new C11 standard will support anonymous structures and unions, see foreword paragraph 6 of the April 2011 draft.

http://en.wikipedia.org/wiki/C1X

The strange part is that both gcc and clang now support anonymous structures and unions in C89 and C99 mode. In my machine no warnings appear.