1. Joint description and joint variable definition
Union is also a new data type, which is a special form of variable.
The description of union and the definition and structure of Union variables are very similar. The format is:
Union union Union name {
Data Type member name;
Data Type member name;
...
} Name of the federated variable;
These variables share the same memory location, saving different data types and variables of different lengths at different times.
The following example indicates a combination of a_bc:
Union a_bc {
Int I;
Char mm;
};
You can then define the federated variables with the specified federated variables.
For example, to define a federated variable named lgc using the preceding description, you can write it as follows:
Union a_bc lgc;
In the combined variable lgc, the integer I and the character mm share the same memory location.
When a union is described, the compiler automatically generates a variable whose length is the maximum variable length in the Union.
The method and structure for joint access to its members are the same. Similarly, federated variables can be defined as arrays or pointers, but when defined as pointers, they must also use the "->;" symbol. In this case, the Federated access member can be expressed:
Union Name> member name
In addition, a union can appear in a structure, and its members can also be structures.
For example:
Struct {
Int age;
Char * ADDR;
Union {
Int I;
Char * Ch;
} X;
} Y [10];
To access the union member I of X in the structure variable Y [1], you can write it:
Y [1]. X. I;
To access the first character of the string pointer ch that joins X in the structure variable Y [2], you can write it as follows:
* Y [2]. X. ch;
If it is written as "Y [2]. X. * Ch;", it is incorrect.
2. Differences between structure and Union
The structure and combination have the following differences:
1) The structure and union are composed of multiple members of different data types, but at any time, the Union conversion only stores one selected member, all the members of the structure exist.
2) assigning values to different members of the Union will be rewritten to other members. The original values of the Members will not exist, but the assignment values for different members of the structure will not affect each other.
The following is an example of how to understand deep integration.
Main ()
{
Union {/* define a Union */
Int I;
Struct {/* define a structure in the Union */
Char first;
Char second;
} Half;
} Number;
Number. I = 0x4241;/* Union member assignment */
Printf ("% C \ n", number. Half. First, mumber. Half. Second );
Number. Half. First = 'a';/* assign values to structure members in the Union */
Number. Half. Second = 'B ';
Printf ("% x \ n", number. I );
Getch ();
}
Output result:
AB
6261
From the above example, we can see that after I is assigned a value, the lower eight bits are the values of first and second. After first and second are assigned a character, the ASCII code of these two characters will also be used as the low-eight-bit and high-eight-bit I.