0

I declared this structure in global scope,

struct a{
int a;
int x;
union b{
int a;
int b;
int y;
};
};

And then declared an union inside main,

union b a;

And this is not giving any errors. But if declare union 'a' in the definition of structure, like:

struct a{
int a;
int x;
union b{
int a;
int b;
int y;
}a;
};

It gives error "duplicate member a". (WE USED THE SAME NAME 'a' IN THE PREVIOUS CASE) Why does one work and another does not?

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:

union b z;  //works in this case, BUT not if we declare 'z' with the definition.
z.y=6;      //works
x=6;        //gives error

(i understand we are declaring union inside main, but its definition in inside the struct. Like, struct.union.union_variable makes sense, but having union.union_variable directly make it kind of independent. Shouldn't it be treated like 'x'?)

nishantbhardwaj2002
  • 757
  • 2
  • 6
  • 18

1 Answers1

5

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;
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
  • But, i am asking that we haven't declared the union independently. We have it inside the struct just like we have 'x'. Why can union be used like that(as if it is not inside any struct, but independently declared)? – nishantbhardwaj2002 May 13 '14 at 11:41
  • @user3340994 are you asking about 1) or 2)? 1) because with that syntax you don't just define an union but also a variable of that type. 2) You can use an integer variable with same name of struct/union member but you have to _declare_ it. – Adriano Repetti May 13 '14 at 11:57
  • i am asking regarding (2) – nishantbhardwaj2002 May 13 '14 at 11:59
  • @user3340994 because in your example you're trying to use a variable named x but it actually doesn't exist. You don't have such variable then it must be declared or used as struct member (and then you need to declare a variable for such struct). See last code snippet. – Adriano Repetti May 13 '14 at 12:01
  • actually i understand the 'x' part. I doubt why the union works. Because we have declared it inside struct. What is the difference between defining a union and using it, and defining a union inside struct and using it. PS- we have directly used union.variable whereas we are supposed to use it like struct.union.variable. – nishantbhardwaj2002 May 13 '14 at 12:06
  • @user3340994 you decide memory layout of an object when you declare a type but you actually allocate memory when you declare a variable of that type. Try to post some code for what you're trying to do and where it doesn't work, like this it'll just be more and more confusing! ;) – Adriano Repetti May 13 '14 at 12:10