When processing data in C language, it is often encountered to split an integer data into high and low, or to combine two characters into one integer data. The usual way to do this is to move the data left or right and combine or split the data. This type of data can now be processed using the properties of unions. Union is also called Commons. All members of a Commons occupy the same memory segment. Modifying one member affects all other members. The memory used by the shared body is equal to the memory used by the longest member. The shared body uses the memory override technique. Only one member’s value can be stored at a time. If a value is assigned to a new member, the original member’s value will be overwritten.

Union (Union), its definition format is: Union common body name {list of members}; Let’s start with an example:

union data{
 long n;
 char ch;
 short m;
 };
Copy the code

N, CH, and M occupy the same memory.



The character ch takes the first byte, the short integer m takes 1 and 2 bytes, and the integer n takes 1, 2, 3, and 4 bytes.

union data{ long n; char ch; short m; }; void main( void ) { union data tem; __asm( "sim" ); // Disable interrupt SysClkInit(); delay_init( 16 ); Uart1_Init( 9600 ); __asm( "rim" ); while( 1 ) { delay_ms( 500 ); tem.ch=0x12; Printf (" # % # % # % x, x, lx \ r \ n ", tem, ch, tem. M, tem, n); }}Copy the code

Assign 0x12 to the character ch and print ch,m,n to see what the output value is. The print result is as follows:



Observe these three values in the simulation



All three values start at the same address in memory. You can see that after assigning 0x12 to the character ch, both m and n have the highest value of 0x12, and the lower value is random.

Assign the value 0x3456 to m and print the result





When m is assigned 0x3456, the value of ch is the highest value of m 0x34, the value of n is the same as the value of m, and the low value of n is random.

Let’s go down and assign n 0x12345678, and see what ch and m are





You can see that when you assign n, both ch and m change. The value of ch is n worth up to 2 bits, and the value of m is n worth up to 4 bits.

Through these tests, it is obvious that the three variables CH, M and N share the same memory address in the memory. When the value of one variable is changed, the value of other variables will also change.

Next, use this feature of the common body to make two character variables and an integer variable occupy the same block of address.

union data{ int n; char ch[2]; }; void main( void ) { union data tem; __asm( "sim" ); // Disable interrupt SysClkInit(); delay_init( 16 ); Uart1_Init( 9600 ); __asm( "rim" ); while( 1 ) { delay_ms( 500 ); tem.n=0x1234; printf("%#x,%#x,%#x\r\n",tem.n,tem.ch[0],tem.ch[1]); printf("------------------------\r\n"); tem.ch[0]=0x56; tem.ch[1]=0x78; printf("%#x,%#x,%#x\r\n",tem.n,tem.ch[0],tem.ch[1]); }}Copy the code

Set an integer variable in the common body, and an array of characters of length 2. So the length of two character variables is exactly the same as the length of an integer variable.



When the value 0x1234 is assigned to the integer n, the character array ch[0] changes to 0x12 and the character array ch[1] changes to 0x34.



The value of the integer variable n happens to be a combination of the character arrays when we assign a value to ch[0] and ch[1] respectively.



The character variable ch[0] is the higher order of n, and the character variable ch[1] is the lower order of n. When you change an integer or a character variable, the value of the other variable also changes.

This allows you to take advantage of the union feature when you need to split an integer into high and low or combine two characters into a single integer.