cgcc

Why do I get an excess element warning in this tagged union in gcc C?


I am writing a tagged union in C, the definition is as follows:

struct uuid_0000000000061d0c {
   long _0;
   union {
      struct {void* _1;};  
      struct {char* _1001; char* _1002;};       
      struct {char* _2001;};
      struct {};
   };
};

There are four anonymous cases in a tagged union. My question is when I initialize this with the "large" two field case I end up with a warning "warning: excess elements in struct initializer".

struct uuid_0000000000061d0c an_instance = { 1, "a", "b" }; 

Following my intuition I successfully restructured the original type definition to avoid the error. Basically, moving the anonymous struct with the largest number of fields to the start of the union fixes this warning.

struct uuid_0000000000061d0c {
   long _0;
   union {
      struct {char* _1001; char* _1002;}; 
      struct {void* _1;};
      struct {char* _2001;};
      struct {};
   };
};

Is this a bug with the warning or am I misunderstanding the intent of the warning?


Solution

  • There are 3 non-conformance issues here: