Comprehensive understanding of structure, federation, and enumeration types _c language

Source: Internet
Author: User

A Structure Body:

1. Define the structure body type:

struct Structural body
{
Any variable of any type;
Any variable of any type;
......
};

Note: This is not a defined variable, but a custom type.

Such as

struct student
{
Char name[10];//student Name
int height;//Student Height
BOOL sex;//Student Sex hypothesis 0 indicates female, 1 indicates male.
};//This disposition number must not be less.

2. Define the structural body variables.

When a type is defined, you can define a variable of that type.

To define a structural body variable:

struct student a,b;//struct can be omitted.
You can assign a value when you define a struct variable.
such as student a={"Liudehua", 172,1},b={"Lixiaolong", 172,1};
You can also assign a value after you define a struct variable, but note that you cannot use {} again.

Such as:

Student A,b;
a={"Liudehua", 172,1},b={"Lixiaolong", 172,1};//this is wrong.

And it should be:
strcpy (A.name, "Liudehua");

a.height=172;
A.sex=1;

3. You can define structural body variables and assign values at the same time as you define the type of struct.

struct student
{
Char name[10];//student Name
int height;//Student Height
BOOL sex;//Student Sex hypothesis 0 indicates female, 1 indicates male.

} a={"Liudehua", 172,1},b={"Lixiaolong", 172,1};

4. To access the structure body:

To access a struct member, use the direct member operator "." or the indirect member operator "->".
Student a={"Liudehua", 172,1};
cout<<a.name<<a.height<<a.sex;
Student *p=&a;
cout<<p->name<<p->height<<p->sex;

For a struct variable, access to the member takes the form of "struct variable. Member", while for the struct pointer, access to the member in the struct variable that it points to, take the form of "structural body pointer-> member".

Two Complex

A federation is also a custom composite type that can contain multiple variables of different types. These variables share a space in memory. The size of this space is the largest variable of the size of each variable.

1 Define the Federation type:

Union myunion
{int num1;
Double num2;
Float num3;
};

Defines a federation type myunion.
The myunion a,b;//defines two myunion variables.
You can also define a federation variable when you define a federation type.

Such as:

Union myunion
{int num1;
Double num2;
Float num3;
}a,b;

How much space does a occupy?
The result of Sizeof (a) is 8, that is, the Myunion occupies 8 bytes, and the same as the double variable.
Note: At any one time, you can only access a variable within the structure.

a.num1=2;
a.num2=3.154;
Myunion *p;
p=&a;
p->num3=5.6;

Three Enum type

1. Definition of enumeration type:
An enumeration type is also a custom compound type. However, members in an enumeration type are constants.

Such as

Enum color

Red
Green
Blue
White
Black
};

The member defaults in the enumeration type start at 0 and sequentially increment. At this point Red==1,green is 2,blue for 3,white for 4,black 5.
You can also change the default value.

Such as

Enum color

Red=1,
Green=3,
Blue=5,
White
Black
};

The value of a member of an enumerated type that is not initialized is incremented on the basis of the member before it.
So White's value is 6, and Black's is 7.

2. Define enumeration variables:

Color A1,A2;

3. Assigning values to enumerated variables:
a1=red;
A2=blue;
cout<<a1<<a2;//output is 15.

Although you enumerate the value integers for a constant, you cannot assign an integer value directly to an enumeration variable.

Such as

a1=1;//this is wrong. Because the type does not match. One is an integral type and one is an enumeration type.
a1= (color) 1;//correct

The size of an enumerated variable is an integer.

The above comprehensive understanding of the structure, federation and enumeration types are small parts to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.