0

I'm trying to define a 4-d matrix type in C (for use in the iOS/ObjC environment) that is encapsulated (so not a bare array), and that can be accessed using indexed values or via named struct members. This is my attempt:

typedef union {
    float m[16];
    struct {
        struct {
            float x;
            float y;
            float z;
            float w;
        } x;
        struct {
            float x;
            float y;
            float z;
            float w;
        } y;
        struct {
            float x;
            float y;
            float z;
            float w;
        } z;
        struct {
            float x;
            float y;
            float z;
            float w;
        } w;
    }; // warning here "Declaration does not declare anything"
} Matrix4;

This works, but I get a warning due to the anonymous (unnamed) struct. I obviously don't want to name that container struct as it only serves to hold the four inner structs.

This page implies that I should be able to do this? http://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html#Unnamed-Fields

It seems to actually work, so is this wrong, or if not, how should I get rid of the warning?

I'm using LLVM GCC 4.2.

Thanks for any insight or suggestions.

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204

1 Answers1

1

Anonymous structs and unions are now allowed (as of C11). Your worries will eventually go away as you migrate to a newer compiler. In GCC, add -std=c1x.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Thanks Kerrek. Do you mean that adding this flag to my current build will change the standard already? Or that I'll need to use a later version of GCC to get this? (And also, this is out of scope for this question, but how likely am I to break other things in my code base by switching to C11, if you happen to know?) – Ben Zotto Feb 04 '12 at 16:25
  • I'm not sure which version of GCC started supporting this dialect; you'd have to check the manual. I doubt that anything would "break"; though you'd have to check the "breaking changes" section of the C11 standard and compare it to the standard which you are currently conforming to. – Kerrek SB Feb 04 '12 at 16:27