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?
As initialization did not specify which union member to use, it attempted to use the first one.
union { struct {void* _1;}; only needs one initializer.
The members are only ever accessed by fields which are given names.
Not in OP's case of initialization. It used the first ones.
Try initializing by name to only ever accessed by fields which are given names.
struct uuid_0000000000061d0c an_instance =
{ ._1001 = "a", ._1002 = "b", ._0 = 1 };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With