How to compile C code with anonymous structs / unions?

solinent picture solinent · Dec 29, 2009 · Viewed 82.1k times · Source

I can do this in c++/g++:

struct vec3 { 
    union {
        struct {
            float x, y, z;
        }; 
        float xyz[3];
    }; 
};

Then,

vec3 v;
assert(&v.xyz[0] == &v.x);
assert(&v.xyz[1] == &v.y);
assert(&v.xyz[2] == &v.z);

will work.

How does one do this in c with gcc? I have

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

But I get errors all around, specifically

line 5: warning: declaration does not declare anything
line 7: warning: declaration does not declare anything

Answer

user287561 picture user287561 · Mar 6, 2010

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

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