The definition and application of the Community

xiaoxiao2021-03-06  80

8.3.1 Community Type With character type variable C, integer variable j There is no need to assign different storage space for two variables, and the two variables can be used to use a storage space, as shown in Figure 8.1. Variables having such storage characteristics are called variables of the common body type. To define a community type variable, you must first define the common body type. The definition method of the common body type is similar to the definition method of the structure type, as long as the keyword Union is used instead of Struct. Definition format: UNION { ; ; ... ;}; where the community type name is composed of identifier, The member name is also consisting of an identifier. Members can be a basic type or export type. Members share a storage area, and the size of the storage area is equal to the maximum value of each member occupies byte length. For example, the community is defined as follows: Union data {char C; INT J;}; where: Data is a common body type name, the community has two members C, J, C as a character type, J is intertwined four Bytes, C and J share the same memory area, the storage area length is four bytes, as shown in Figure 8.1.

8.3.2 Definition of Community Type Variables The definition of the common body type variable is exactly the same as the definition of structural variables, and there are three forms: (1) First define the type, then define the variable. (2) Define variables while defining the type. (3) Directly define a common type variable. [Example 8.8] The common body type variable is defined in three ways. # Include union data {char c; int j;}; data d; // First define type, then define variables. Union data1 {char C1; INT J1;} X1, x2, x3; // Define variables while defining the type. Union {char C; INT I; float x;} a, b, c; // directly define the common body type variable. Main () {DC = 'a'; DJ = 98; Cout << DC << Endl;} Execution Result is: b In the main function, the character type member C of the Community Variable D is assigned characters 'a' value. The integer member J of the common body variable D is assigned to 98. Since J and C take advantage of the same memory space, as shown in Figure 8.1. Therefore, after performing assignment statement D.j = 98, the content of D.C has been modified to 'b' ASCII code 98, so when COUT << D.c is finally executed, B, not A.

8.3.3 The reference to the community variable of the common body variable is the same as the reference method of the structural variable, and the member operator "." Connection variable name and member name can be used, and its reference format is: . As in the above example, DC and DJ are references to members C, J of the common body variable D. The main purpose of using a common type variable is to reduce the amount of program variables on memory. Today, computer memory is getting bigger, so memory capacity is no longer considered in programming. Therefore, the community type variable is less and less.

转载请注明原文地址:https://www.9cbs.com/read-92517.html

New Post(0)