Detailed discussion of structure struct union Union and enum Enumeration

Source: Internet
Author: User

Union)
Is a data type. It provides a way to share buckets between members of different types of data, and can realize automatic type conversion between members of different types of data. A consortium object can only store values of one member at a time.
The size of the memory of the Union depends on the members with the largest number of bytes, rather than the sum. The Union also performs the font alignment. You can specify an initial value when defining a federated variable, but only one initial value can be specified (the initial value cannot be set after testing). The type of the initial value must match the type of the first member of the Union. You can get the address of a federated variable or the address of any member in the Variable. They are always equal. Values can be assigned between the same type of Federated variables, but the size of the two federated variables cannot be compared, not only because there may be a problem of filling the bytes, but these two variables may be members of different types, so it represents two different types of variables.

There are several ways to define a consortium:

1) Standard Method

Union
{
Int B;
Char C;
};
A abc;
ABC. B = 4;
ABC. c = 5;
// Or you can define variables directly during definition.
Union
{
Int B;
Char C;
} ABC;
ABC. B = 4;
ABC. c = 5;

2) use typedef. This is equivalent to redefining a custom type, and a serves as the type name. Instead of the above variable name.

Typedef Union
{
Int B;
Char C;
};
A abc;
ABC. B = 4;
ABC. c = 5;

3) define the Instance name instead of the type. This method is used to use Union locally, but only in a specified scope. ABC is only the variable name, not the type name.

Union
{
Int B;
Char C;
} ABC;
ABC. B = 5;
ABC. c = 6;

4) Anonymous Consortium (Anonymous Union), This kind of consortium is special. It neither defines the type name nor the variable name. This consortium can only work within struct, which is equivalent to a member of the structure.

Struct
{
Int B;
Union
{
Int C;
Char D;
};
};
A abc;
ABC. B = 5;
ABC. c = 6;
ABC. d = 7;

There is a simple program that uses a consortium to determine the platform attributes. It can quickly determine whether the platform is a big-end or a small-end model. If the function returns 0, it indicates that the platform is in the big-end mode; otherwise, it is in the small-end mode.

1int checkcpu (void)
2 {
3 Union W
4 {
5 int;
6 char B;
7} C;
8 C. A = 1;
9 return (C. B = 1 );
10} Structure (struct)Struct is used in many ways. The method used is similar to union. It can be defined by standard, and typedef can be used to define aliases. Generally, the structure is defined using a more standard method such as typedef. You can also define only one local structure variable without defining the type name, so that the structure can be used only in a small range. In addition, the structure also has Anonymous structure (anonymous struct)But it is not widely used. c ++ does not support anonymous structures. Its usage is opposite to Union, and must be placed inside the Union body before it can be used. Union U {
Struct {
Int;
Double B;
};
Struct {
Char * C;
Unsigned D;
};
};

Enumeration (Enum)
Defines a set of special purpose Symbolic constants, which indicate the range of values that can be taken for such types of variables.
When defining the enumeration type, if you do not specify the value of the identifier, the value of the first identifier will be 0, followed by 1 greater than the previous one. If the value of a token is specified, the value of the marker is 1 in sequence. For example
Enum week {sun, MON = 125, Tue, wed, Thu = 140, Fri, sat };
The value of the symbolic constant is 0,125,126,127,140,141,142 in sequence.
You can also initialize some enumerated constants to equal constants, such
Enum ABC {A = 1, B = 1, C = 100 };
In addition, the enumeration type can be anonymous, that isAnonymous Enumeration(Anonymous Enum). An anonymous enumeration type is equivalent to a directly defined const symbolic constant. It can be used as a global enumeration or placed in any class definition or namespace.
Enum
{
Object_creationg = 0x10;
Object_deletiong = 0x11;
State_change = 0x12;
Attr_value_change = 0x13;
};
It can replace macro constants and const symbolic constants.

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.