"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. The union definition defines a union type in the general form: union name {member table}; member table contains several members, the general form of members is: the name of a member must comply with the requirements of the identifier. For example: union perdata {int class; char office;}; defines a union type called perdata. It contains two members, one being an integer and the other being a class. The other is a character array, the array name is 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. There are also three forms. 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 ;}; union perdata a, B;/* describes, B is of the perdata type */or can be described as: union perdata {int class; char office;} a, B; or: union {int class; char office ;} variables a and B are of the perdata type. Their memory allocation is 7-8. 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. It can be seen that variables a and B use only two bytes for an integer value, and 10 bytes for a character array. The value assignment and use of joint variables can only be used for members of 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 the federated variables. The assignment can only be performed in the program? Why can't I find myself running? Can a federated variable be assigned only one member value at a time? Route 8? 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. [Code: 1: 8d8ee8c82c] main () {struct {char name [10]; int age; char job; union {int class; char office [10];} depa ;} 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. class); else scanf ("% s", body [I]. depa. office);} printf ("name/tage job class/office/n"); for (I = 0; I <2; I ++) {if (body [I]. job ='s ') printf ("% s/t % 3d % 3c % d/n", body [I]. name, body [I]. age, body [I]. job, body [I]. depa. class); else printf ("% s/t % 3d % 3c % s/n", body [I]. name, body [I]. age, body [I]. job, body [I]. depa. office) ;}} [/code: 1: 8d8ee8c82c] This example uses a structure array body to store personnel data. This 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, note that any member of the array type, whether a structure member or a federated Member, cannot add the "&" operator before the entry. For example, the body [I] In the second line of the program. name is an array type. The body [I] in row 22nd. depa. office is also an array type, so 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: Chapter 1. Structure and union are two types of data, which are an important means for users to define new data types. There are many similarities between the structure and the Union, which are composed of members. Members can have different data types. The member representation is the same. Three methods are available for variable description. 2. In the structure, each member occupies its own memory space and they exist at the same time. The total length of a structure variable is equal to the sum of all member lengths. In the Union, all Members cannot occupy the memory space at the same time, and they cannot exist at the same time. The length of the federated variable is equal to the length of the longest member. 3. "." is a member operator, which can be used to represent Member items. The member can also be expressed using the "->" operator. 4. The structure variable can be used as a function parameter, and the function can also return a pointer variable pointing to the structure. Union variables cannot be used as function parameters, and functions cannot return pointer variables pointing to union. However, you can use a pointer to a federated variable or a federated array. 5. The structure definition can be nested, and the structure can also be associated as members to form the nesting of the structure and union.