I have the following struct:
struct type1 {
struct type2 *node;
union element {
struct type3 *e;
int val;
};
};
When initialising a pointer *f
that points to an instance of type1
and doing something like:
f.element->e
or even just f.element
, I get:
error: request for member ‘element’ in something not a structure or union
What am I overseeing here?
element
is the name of the union, not the name of a member oftype1
. You must giveunion element
a name:then you can access it as:
If f is a pointer, then you may access "element" using f->element, or (*f).element
Update: just saw that "element" is the union name, not a member of the struct. You may try
So the final struct would be like this:
And now you can access element members like this, through a type1 *f: