Definition and use of C language keyword Union

Time:2021-7-30

Union, Chinese name “union, common body”, is a data structure similar to the structure struct to some extent. The common body (Union) and structure (struct) can also contain many data types and variables.

But in Union,   Each member shares a memory space, and the length of a joint variable is equal to the longest length of each member  。 A consortium type must be defined before it can be used, and a variable declaration can be defined as the consortium type.

When defining structure objects, if they are not explicitly initialized, the general initialization rule is adopted: if the structure object belongs to dynamic storage type, its members have uncertain initial values.

On the other hand, if the structure object has a static storage cycle, its non pointer member initial value is 0 and its pointer member initial value is a null pointer.

Unlike structure members – they all have separate memory locations in the structure, and federated members share the same memory location. That is, all members in the union start at the same memory address. Therefore, a union with many members can be defined, but only one member can contain one value at a time. Federation allows programmers to easily use the same memory location in different ways.

 

1、 Define Union

The union is defined in the same way as the structure, except that the keyword struct is changed to Union:

Union [tag name] {member declaration list};

The following example defines a union type named data, which has three members: I, x, and STR:

union Data { int i;double x;char str [16];};

This type of object can store an integer, a floating point number, or a short string.

For example:

union StateMachine 

   char character; 

   int number; 

   char *str; 

   double exp; 

}; 

A union is only configured with a large enough space to accommodate the data members with the maximum length. In the above example, the maximum length is the double type, so the space size of Statemachine is the size of the double data type.

In C + +, the default property page of union members is public. Union is mainly used to compress space. If some data cannot be used at the same time, you can use union.

2、 Initialize Union

Similar to the structure, the union object is initialized through the initialization list. However, for unions, the list has only one initializer.

Like the structure, C99 allows the use of a member indicator in the initializer to indicate which member is initialized. Also, if the initializer does not have a member indicator, it is associated with the first member in the Federation. Federated objects with automatic storage classes can also be initialized with existing objects of the same type. Here are some initialization examples:

union Data var1={77},

var2={.str=”Mary”},

var3=var1,

myData[100]={{.x=0.5},{1},var2};

Elements in the array mydata are implicitly initialized to a value of 0 if no initializer is specified.

3、 Declaration of consortium variables

The description of joint variables is the same as that of structural variables, and there are three forms:

First kind  ,  Define the union type first, and then declare the union variable  ;

Second  , Define the consortium type and declare the consortium variable at the same time;

Third  , Declare a union directly (define the union type anonymously).

Taking perdata type as an example, the description is as follows:

First,   Define the union type first, and then declare the union variable

union   perdata

{

   int class;

   char officae[10];

};

union     perdata    a,b;   /* Description A and B are perdata types*/

Second,   Declare the consortium variable while defining the consortium type:

union  perdata

int class;

char office[10]; 

} a , b ;

Third,   Declare a union directly (define the union type anonymously):

union

int class;

char office[10]; 

}a,b 

The explained a and B variables are of perdata type. a. The length of the B variable should be equal to the longest length in the perdata member, that is, the length of the office array, a total of 10 bytes. When assigning integer values to a and B variables, only 2 bytes are used, while when assigning character arrays, 10 bytes can be used.

4、 Assignment and use of consortium variables

The assignment and use of consortium variables can only be performed on the members of variables.

The members of the union variable are expressed as:    Joint variable name  .  member name

For example, after a is described as a variable of perdata type, you can use a.class and a.office

Assignment or other operations using only joint variable names are not allowed. It is also not allowed to make initial assignment to joint variables, and the assignment can only be carried out in the program.

A consortium variable can only be given one member value at a time. The value of a joint variable is a member value of the joint variable.

5、 Differences between struct and union

1. Both structure and union are composed of multiple members of different data types, but only one selected member is stored in the union at any time, and all members of the structure exist.

2。 Assigning values to different members of the Union will overwrite other members, and the value of the original member will not exist, but assigning values to different members of the structure will not be affected.

The assignment of a joint variable can only be made to the members of the variable. A joint variable can only be assigned to one member at a time. The value of a joint variable is a member value of the current joint variable.

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 any member value, but only one value can be assigned at a time. If a new value is assigned, the old value will be offset.

 

If you want to better improve your programming ability, learn C language and C + + programming well! Overtake at the corner, take a quick step!

C language c + + Learning Circle】, sharing (source code, project actual combat video, project notes, basic introductory tutorial)

Welcome to change careers and learn programming partners. Use more materials to learn and grow faster than you think!

Programming learning books:

 

Programming learning video: