Discussion Locked

C:UNION

0 helpful
8 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
We know that in C union stores different variables in the same mamoty location, if so then then if i initialialize all of them then why it is not overwriting?..How it can store different values in same memory location??

8 replies

by rvk

You can store only one value at any given location at any given time. By using the unions, you can interpret the contents of that location differently.
Eg:
union {
short s1;
char xyz[sizeof(short)];
}my_union;

you can store some value in
my_union.s1 = 0x0102; // a numeric value in Hex

u can see the contents of same location as 'chars' by
my_union.xyz[0], my_union.xyz[1]
Regards
rk

by chandraditya2001

sorry, but i am in the same position as i was before getting the answer. my be Mr. rk had not understood my question properly.

by rvk

Yes.. Probably I did not understand your question.
Here is a sample pgm...
union {
char x[20];
char y[10];
}u1;
memset((void *) &u1,0, sizeof (u1));
strcpy (u1.x,"ABCDEFGHIJKLMNOPQRST");
printf("before initialization\n");
printf("\nx: %s\n", u1.x);
printf("\ny: %s\n", u1.y);
memset(&u1.y[0],' ',10);
printf("after initialization\n");
printf("\nx: %s\n", u1.x);
printf("\ny: %s\n", u1.y);

Results are:
before initialization
x: ABCDEFGHIJKLMNOPQRST
y: ABCDEFGHIJKLMNOPQRST
after initialization
x: KLMNOPQRST
y: KLMNOPQRST

Please see if this answers the question if not please state it again.
Thanks & Rgds
rk

by rvk

The results after initialization will have spaces up to 10 chars width. This looks like got truncated as I pasted the results here.
The o/p after initilization would be

x:" KLMNOPQRST"
y:" KLMNOPQRST "
Thanks

by shajithchandran

hi,
First of all let me admit that i didn't understand ur question properly. Even than i will try.

First of all you cannot store two different thing in the same memory location. But u can use union to store different values but that won't be in the same memory location.

For example if you want to store an int and a char in the union than u can do it in the following manner.

union
{
int a[2];
char c;
};

In this case a[0] and c will be overlapping. (This clearly depends on the byte ordering used by the processor. It can be the other ways round also.) Now in the union a[1] can be used to store an int and c can be used to store char.

Let be frank with u. This is not why union are meant for. U can use a structure instead. Union is mainly used to interpret same data in different manners.
I will give u an eg.
suppose u have four characters. Now ur requirment is that u have combine these four chars to form a long int. This thing can be easily by using an union. (Although there are many other ways to do this.)
union
{
char c[4];
long l;
}

Now whenever u want the char use union.c[] and when u want them to be interpreted as long than use union.l .

I hope ur doubt is cleared. If not than pls mention ur doubt in detail.

This discussion is closed to new replies.