"Union" has some similarities with "structure. But they are essentially different. Each member has its own memory space in the structure. The total length of a structure variable is the sum of the member lengths. In "Union", each member shares a piece of memory space. The length of a federated variable is equal to the longest length of each member. It should be noted that the so-called sharing here does not mean that multiple members are loaded into a joint variable at the same time, but that the joint variable can be assigned to any member value, however, only one value can be assigned at a time. If a new value is assigned, the old value is washed away. For example, if the "unit" variable described above is defined as a Union that can be attached to a "Class" or "Teaching and Research Section", an integer value (class) or a string (Teaching and Research Section) is allowed ). Either assign an integer value or a string, but not both. The definition of the Union type and the description of the Union variable must be defined before the variable can be described as the Union type.
1. Definition of union
The general form of defining a union type is:
Union name
{
Member table
};
The member table contains several members. The general form of the members is: the name of the member name of the type specifier should comply with the requirements of the identifier.
For example:
Union perdata
{
Int class;
Char office [10];
};
Defines a union type named perdata, which contains two members, one is an integer, the member is a class, the other is a character array, and the array is named office. After joint definition, you can describe the joint variables. variables described as perdata types can be used to store integer classes or character arrays.
2. Description of Federated Variables
The description of Federated variables is the same as that of structural variables. That is, definitions are defined first and then explained. definitions are both described and directly described. Taking the perdata type as an example, it is described as follows:
Union perdata
{
Int class;
Char officae [10];
};
Union perdata a, B;/* indicates that a and B are of the perdata type */
Or it can be described as follows:
Union perdata
{
Int class;
Char office [10];
}
A, B; or directly:
Union
{
Int class;
Char office [10];
}
A, B
The variables a and B are of the perdata type. The length of the and B variables should be equal to the longest length of the perdata Member, that is, the length of the office array, a total of 10 bytes. For example, if a and B variables are assigned an integer value, only two bytes are used. If a character array is assigned, 10 bytes can be used.
Assignment and use of joint Variables
Assign values to the federated variables only to the members of the variables. The federated variable member is the name of the federated variable. for example, after a is described as a variable of the perdata type, you can use. class. office cannot assign values or perform other operations only by using the name of the federated variable. It is not allowed to initialize and assign values to federated variables. Values can only be assigned to programs. It must be emphasized that only one member value can be assigned to a federated variable at a time. In other words, the value of a federated variable is a member value of the federated variable.
[Example 7.15] There is a general table for teachers and students. The instructor data includes four items: name, age, occupation, and teaching and research section. There are four students: name, age, occupation, and class.
Program and input personnel data, and then output in a table.
# Include <stdio. h>
Struct stu
{
Char name [10];
Int age;
Char job;
Union
{
Int classes;
Char office [10];
} Depa;
};
Void main ()
{
Struct stu body [2];
Int n, I;
For (I = 0; I <2; I ++)
{
Printf ("input name, age, job and department \ n ");
Scanf ("% s % d % c", body [I]. name, & body [I]. age, & body [I]. job );
If (body [I]. job ='s ')
Scanf ("% d", & body [I]. depa. classes );
Else
Scanf ("% s", body [I]. depa. office );
}
Printf ("name \ tage \ tjob \ tclass/office \ n ");
For (I = 0; I <2; I ++)
{
If (body [I]. job ='s ')
Printf ("% s \ t % 3d \ t % 3c \ t % d \ n", body [I]. name, body [I]. age, body [I]. job, body [I]. depa. classes );
Else
Printf ("% s \ t % 3d \ t % 3c \ t % s \ n", body [I]. name, body [I]. age, body [I]. job, body [I]. depa. office );
}
}
In this example, a structure array body is used to store personnel data. The structure has four members. The member item depa is a union type, which consists of two members, one being an integer class and the other being an array of characters. In the first for statement of the program, enter the data of the person, first enter the name, age, and job of the first three members of the structure, and then identify the job member items, if it is "s", enter the Union depa · class (assign the class number to the student) or the depa · office (assign the teaching and research group name to the teacher ).
When using scanf statements for input, note that any member of the array type, whether a structure member or a federated Member, cannot add the "&" operator before the entry.. Such as program
Body [I]. name is an array type, and body [I]. depa. office is also an array type. Therefore, the "&" operator cannot be added between the two items. The second for statement in the program is used to output the values of each member item.