It gives error "duplicate member a". (WE USED THE SAME NAME 'a' IN THE PREVIOUS CASE) Why does one work and another does not?
Because your struct has now two members named a
: first one is an int
and second one is of type b
(your union
). You wouldn't be surprised to see this doesn't compile:
struct a {
int a;
float a;
};
In your case you have exactly same situation, imaging you defined b
outside struct
and you try to use it like this:
struct a {
int a;
union b a;
};
Secondly, how can we use the union declared inside struct, independently, but can't use any other integer variable, say 'x'? Like, i can perform the following successfully:
Where is x
declared? You don't have any local variable named x
(what you may have is a struct member named x
but then you need such structure). One of following (according to what you're trying to do):
int x = 6;
struct a w;
a.x = 6;