Struct definition and initialization

Source: Internet
Author: User

Objective: struct type, struct variable, and struct initialization. Clarify the relationships between these three concepts.

 

Structure Definition

The general form of defining a structure is:

Struct structure name

{

Member Columns

}

A member table consists of several members, each of which is an integral part of the structure.

Each member must also be described as a type.

For example:

Struct Stu

{

Int num;

Char name [20];

Int age;

}

Description of structure type variables

Struct definition does not define a variable, but definesData TypeThis type is defined by you. It can be used like all the simple data types of the language itself (such as INT ).

The struct itself is not used as data to open up memory. What is actually stored in the memory as data is defined by this struct.Variable.

How much memory does a struct variable occupy? This is determined by the definition of this struct type. It can be imagined that in order to store every member of the struct at the same time, the storage size of the struct variable should be the total storage space of all components.
It indicates that there are three methods for Structure Variables. The Stu defined above is used as an example to describe it.

  1. Define the structure first, and then describe the structure variables. For example:

Struct Stu

{

Int num;

Char name [20];

Int age;

};

Struct Stu boy1, boy2;

The two variables boy1 and boy2 are of the stu structure type.

You can also use a macro definition to make a symbolic constant to represent a structure type. For example:

# Define Stu struct Stu

Stu

{

Int num;

Char name [20];

Int age;

};

Stu boy1, boy2;

  1. Description of the Structure Variable while defining the structure type. For example:

Struct Stu

{

Int num;

Char name [20];

Int age;

} Boy1, boy2;

  1. Directly describe the structure variables.

For example:

Struct

{

Int num;

Char name [20];

Int age;

} Boy1, boy2;

The difference between the third method and the second method is that the structure name is omitted in the third method, and the structure variable is directly given.

After the boy1 and boy2 variables are of the stu type, values can be assigned to each member of the two variables.

In the above Stu structure definition, all Members are of the basic data type or array type. A member can also be a structure, that is, a nested structure.

For example:

Struct Date {

Int month;

Int Day;

Int year;

}

Struct {

Int num;

Char name [20];

Struct date birthday;

} Boy1, boy2;

The common form of using structure variable members is:

Structure variable name. member name

For example:

Boy1.num

/* The student ID of the first person */

If the member itself is a structure, you must find the lowest-level member level one by one to use it.

Example: boy1.birthday. Month

That is, the month member of the first person's birth can be used independently in the program, which is exactly the same as the common variable.

Structure Variable initialization: similar to multi-dimensional array initialization.

Structure Variable assignment

You can assign values to struct variables one by one. This method is similar to arrays. arrays assign values to elements one by one (for arrays, unless initialized, you have no choice but to assign values in this way ).Unlike arrays, struct variables in Standard C can be assigned an integral value..

Example 1:

# Include <stdio. h>

Int main (void)

{

Struct student

{

Long int num;

Int age;

Char * Name;

} ST1 = {200,18, "zhangsan "};

Struct student st2, st3;

Printf ("No. Age name/N ");

Printf ("% LD % d % s/n", st1.num, st1.age, st1.name );

Getch ();

St2.num = 199;

St2.age = 19;

St2.name = "Lisi ";

Printf ("% LD % d % s/n", st2.num, st2.age, st2.name );

Getch ();

St3 = st2;

Printf ("% LD % d % s/n", st3.num, st3.age, st3.name );

Getch ();

Printf ("/n struct Student: % d", sizeof (struct student ));

Getch ();

Return 0;

}

Example 2:

# Include <stdio. h>

# Include <conio. h>

Struct birth

{

Int year;

Int month;

Int Day;

};

Struct student

{

Long int num;

Struct birth birthday;

Char * Name;

} ST1 = {200, {, 8}, "zhangsan "};

Int main (void)

{

Struct student st2;

St2 = ST1;

Printf ("% LD % S % d/N", st2.num, st2.name, sizeof (INT ));

Printf ("Year: % d month: % d/N ",

St2.birthday. Year,

St2.birthday. Month,

St2.birthday. Day );

Getch ();

Return 0;

}

Note:

When your struct variable contains multiple characters, we recommend that you define it as an array (for example, the previous name member,I don't know how large the array should be to be opened. It can also be defined as a pointer.). The reason is that pointer variables cannot store actual data, only addresses.

 

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.