Basic C question about union

Status
Not open for further replies.

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,

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)
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

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.
 

the effect of
Code:
Union_Test.value = 10;
Union_Test.address = 1212;
would depend on the sizeof an int which the C standard specifies as a minimum of 16 bits so it may be 16, 32 or 64 bits etc.
if an int is 32 bits and long is 32 bits ( a minimum of 32 bits) the value of Union_Test.value would be 1212
however, consider
Code:
   Union_Test.address = 12121212;
    Union_Test.value = 10;
if int and long are 32 bits the value of Union_Test.address is 10
however, if int is 16 bits and long is 32 bits only 16 bits of Union_Test.address will have been changed, e.g. using the Turbo C DOS compiler its value was -2948
 

when you declare

Code C - [expand]
1
2
3
4
5
union Test{
char ch;
int value;
long address;
}Union_Test;



the union size will be the one of the longest variable type,
in this case you have an 8bit char, 16bit int (probably) and 32bit long.
so the value will be 32bit.

Depending on the value you assign the value to , different bits will be written ,
when you assign a value to char ch then only the red bits will be written
when you assign a value to int value then only the blue bits will be written
when you assign a value to long address then all the green bits will be written,

all the other bits will remain unchanged and the (32 bit) value will be wrong if you write a char value over the long value (because the higher bits will keep the old value).

00000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000

Alex
 
A useful way to use unions is to read a variable of one type with another type.
For example you can have


Code C - [expand]
1
2
3
4
union Test{ char mychar[4];  // array of char 4x(8bit), total 32bits long
            long mylong;              // 32bit
                        
            }Union_Test;



if we assume that you have some kind of device that sends a 32bit value in 8bit parts (using SPI or a port with 8pins) then you can assign the four received chars to the array of chars and then read it as a long value.

or the opposite, for example you can have some variable calculated inside the mcu and you want to send it in 8bit parts through the uart interface, using a union like

Code C - [expand]
1
2
3
4
union Test{ char mychar[2];  // array of char 2x(8bit), total 16bits long
            integer myinteger;      // 16 bit
                        
            }Union_Test;


then you can assign the integer value to myinteger variable and then use the mychar array to send the high and low byte through uart.

Alex
 
Last edited:
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…