"Union" has some similarities between "structure". But both have different inherent. In the structure, each member has its own memory space, the total length of a structural variable is the sum of each member length. In the "United", each member share a memory space, and a combined variable is equal to the longest length in each member. It should be noted that this so-called sharing does not mean that multiple members are simultaneously loaded into a combined variable, but means that the combined variable can be given to any member value, but only one value can be assigned each time. The value is rushed away. As the "unit" variable described earlier, if it is defined as a combination of "class" or "teaching,", it allows the intersection (class) or a string (teaching and research room). Either give an integer value, or give a string, you cannot give it simultaneously. The definition of the federal type and a combined variable describes that a joint type must be defined to explain the variable as the federated type.
First, the union definition
Define a general form of a joint type:
Union United Name {member table};
The membership table contains several members, and the general form of members is: Naming of the type indicator member name should meet the identifier.
E.g:
Union Perdata {Int Class; Char Office [10];
Define a federated type named PerData, which contains two members, one for integers, member names; another array, the array is named office. After the joint definition, a combined variable description can be performed as a variable of the PerData type, and an integer Class or a character array office can be stored.
Second, the joint variable
The description of the combined variables and the structural variables are the same, and there are three forms. That is, first defined, then explain; definitions simultaneously explain and directly explain. Take the PERDATA type as an example, which is as follows:
Union Perdata {Int Class; Char OffICAE [10];}; Union PerData A, B; / * Description A, B is PerData Type * /
Or may indicate:
Union Perdata {Int Class; Char Office [10];
A, B; or directly explain:
Union {int class; char office [10];} a, b
The A and B variables after the instructions are Perdata types. Their memory assignment is shown in Figure 7-8. A, the length of the B variable should be equal to the longest length in the members of PerData, which is equal to the length of the Office array, a total of 10 bytes. As can be seen from the figure, if the B variables are given to the integer value, only two bytes are used, and 10 bytes can be used when applying the character array.
Assignment and use of joint variables
The assignment of the combined variable can only be carried out by a member of the variable. Members of the combined variable are represented as: a combined variable name. Member name, for example, a variable that is illustrated as a PerData type, can use A.CLASS A.Office not allow only combined variable name to assign values or other operations. The initialization assignment of the combined variable is also not allowed to assign only in the program. Also emphasized that a combined variable can only give a member value each time. In other words, the value of a combined variable is a member value of a joint committee.
[Example 7.15] There is a form of teacher and student, and teachers have four names, age, occupations, and teaching and research. The students have four names, age, occupation, and class.
Program input person data, then output it in a table.
Main () {structure {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 . ofTF ("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);}} This example A structural array body is stored in person data, which has four members. The member DEPA is a joint type, which is composed of two members, one for integer Class, one is a character array office. In the first For statement of the program, enter the data of the person, first enter the top three members name, agents of the structure, and then discriminate Job member items, such as "S", the joint DEPA · Class input ( Duty number of the students) Otherwise, the DEPA · Office input (the team name is called to the teacher).
Be careful when entering the SCANF statement, any member of the array type, whether it is structural member or a joint member, before the "&" operator can be added before this. If Body [i] .Name in line 18 is a set of array types, Body [i]. DEPA.Office in line 22 is also an array type, so "&" operator cannot be added between the two items. The second for statement in the program is used to output the value of each member item.