dhanraj_kmr
Advanced Member level 4
- Joined
- Sep 23, 2008
- Messages
- 117
- Helped
- 3
- Reputation
- 6
- Reaction score
- 3
- Trophy points
- 1,298
- Location
- Chennai, INDIA
- Activity points
- 2,174
Hi to all,
I have a doubt about using Union in C. That is
An union declaration is,
A global variable declaration is,
If i understood the use of union properly, i can have memory space for union is
which is maybe 4 bytes. And size of the global variable also taking the same amount of space.
in a union i can access only variable at a time so if i assign
so second assignment removed Union_Test.value's value and assigned 1212 in the place. so the whole 4bytes of memory space contains the value of Union_Test.address if i am not wrong.
So the same behavior can happen with normal global variable Gl_var.
So whats the difference between these two and give more information if you think this topic will be useful to others.
I have a doubt about using Union in C. That is
An union declaration is,
Code:
union Test{
char ch;
int value;
long address;
}Union_Test;
A global variable declaration is,
Code:
long Gl_Var;
If i understood the use of union properly, i can have memory space for union is
Code:
sizeof(Union_Test.address)
in a union i can access only variable at a time so if i assign
Code:
Union_Test.value = 10;
Union_Test.address = 1212;
so second assignment removed Union_Test.value's value and assigned 1212 in the place. so the whole 4bytes of memory space contains the value of Union_Test.address if i am not wrong.
So the same behavior can happen with normal global variable Gl_var.
So whats the difference between these two and give more information if you think this topic will be useful to others.