I have a struct:
typedef struct _n
{
int type;
union {
char *s;
int i;
};
} n;
When I try to assign a compound literal, like:
node n1 = {1, 0};
node n2 = {2, "test"};
gcc gives me some warnings such as:
warning: initialization makes pointer from integer without a cast
warning: initialization from incompatible pointer type
Well, it's clear that the compiler isn't sure about me just assigning a value to a possibly ambiguous type. However, even if I try to specify more precisely:
node n0 = {type: 1, i: 4};
I get:
error: unknown field ‘i’ specified in initializer
I have read that if I put (union <union name>)
before i:
then it may work. However, I prefer having an anonymous union. Is there a way to do it?