C language data type: Union)

Source: Internet
Author: User

1. Joint description and joint variable definition
Union is also a new data type, which is a special form of variable.
The description of union and the definition and structure of Union variables are very similar. The format is:
Union union Union name {
Data Type member name;
Data Type member name;
...
} Name of the federated variable;
Federated several variables to share a memory location and save different data types at different times
And variables of different lengths.
The following example indicates a combination of a_bc:
Union a_bc {
Int I;
Char mm;
};
You can then define the federated variables with the specified federated variables.
For example, to define a federated variable named lgc using the preceding description, you can write it as follows:
Union a_bc lgc;
In the combined variable lgc, the integer I and the character mm share the same memory location.
When a union is described, the compiler automatically generates a variable whose length is the maximum value in the Union.
Variable Length.
The method and structure for joint access to its members are the same. Likewise, federated variables can be defined as arrays or pointers,
But when it is defined as a pointer, the "->" symbol is also used. In this case, the Union access member can be expressed:
Union Name> member name
In addition, a union can appear in a structure, and its members can also be structures.
For example:
Struct {
Int age;
Char * ADDR;
Union {
Int I;
Char * Ch;
} X;
} Y [10];
To access the union member I of X in the structure variable Y [1], you can write it:
Y [1]. X. I;
To access the first character of the string pointer ch that joins X in the structure variable Y [2], you can write it as follows:
* Y [2]. X. ch;
If it is written as "Y [2]. X. * Ch;", it is incorrect.

2. Differences between structure and Union
The structure and combination have the following differences:
1. The structure and union are composed of multiple members of different data types, but at any time,
Only one selected member is stored in the Union, and all Members in the structure exist.
2. assign values to different members of the Union, and rewrite the values of the original members.
However, the assignment of different members of the structure does not affect each other.
The following is an example of how to understand deep integration.
Example 4:
Main ()
{
Union {/* define a Union */
Int I;
Struct {/* define a structure in the Union */
Char first;
Char second;
} Half;
} Number;
Number. I = 0x4241;/* Union member assignment */
Printf ("% C/N", number. Half. First, mumber. Half. Second );
Number. Half. First = 'a';/* assign values to the structure members in the Union */
Number. Half. Second = 'B '';
Printf ("% x/N", number. I );
Getch ();
}
Output result:
AB
6261
From the above example, we can see that after I is assigned a value, the lower eight bits are the values of first and second;
After the first and second characters are assigned, the ASCII code of these two characters will also be used as the low-eight-bit and high-eight-bit of I.
Bit.

Enum)

Enumeration is a set of named Integer constants, which are common in daily life.
For example, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday is an enumeration.
The description of enumeration is similar to the structure and union in the form:
Enum enumeration name {
Identifier [= integer constant],
Identifier [= integer constant],
...
Identifier [= integer constant],
} Enumeration variables;
If the enumeration is not initialized, that is, when the "= integer constant" is saved
Assigned to identifier 0, 1, 2 ,.... However, when a member in the enumeration is assigned a value, the subsequent members are sorted in sequence.
The value is determined based on the rule of adding 1.
For example, after the following enumeration, the values of x1, x2, X3, and X4 are 0, 1, 2, and 3, respectively.
Enum string {x1, x2, X3, X4} X;
When the definition is changed:
Enum string
{
X1,
X2 = 0,
X3 = 50,
X4,
} X;
Then X1 = 0, X2 = 0, X3 = 50, X4 = 51
Note:
1. In the enumeration, each member (identifier) Terminator is ",", not ";". The last member can be omitted.
",".
2. A negative value can be assigned during initialization, and the subsequent identifiers will be incremented by 1.
3. The enumerated variable can only be an identifier constant in the enumeration description structure.
For example:
Enum string
{
X1 = 5,
X2,
X3,
X4,
};
Enum strig x = X3;
In this case, the enumerated variable X is actually 7.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.