In practical problems, a set of data often has different data types. For example, in the student registration form, the name should be a character type; the student number can be integer or a character; the age should be integer; gender should be a character type; the score can be integer or real. It is obvious that this group of data cannot be stored in an array. Because the type and length of each element in the array must be consistent to compile the system processing. In order to solve this problem, another construction data type - "Structure" is given in the C language. It is equivalent to records in other advanced languages.
"Structure" is a type of constructor, which is composed of several "members". Each member can be a basic data type or another constructive type. The structure is both a "constructed" data type, then defined it before explaining and use, that is, construct it. The function is defined before the function is explained and called the function.
Structure definition
Define a general form of a structure:
Struct structure name {member table column};
Members are composed of several members, each member is an integral part of the structure. For each member, it must also be described in the form of:
Type indicator member name;
The name of the member name should meet the writing of the identifier. E.g:
Struct stu {int Num; char name [20]; char sex; float score;
In this structure definition, the structure is called STU, which consists of four members. The first member is NUM, integer variable; the second member is Name, the character array; the third member is SEX, the character variable; the fourth member is Score, real variable. It should be noted that the semicolon after parentheses is indispensable. After the structure is defined, the variable will be performed. Any variable indicating that the structure STU is composed of the above four members. It can be seen that the structure is a complex data type, which is a collection of number of ordered variables that are fixed and type different.
Description of structural type variables
The structural variable has the following three methods. The STU defined above is an example.
1. First define the structure and then explain the structural variable. Such as:
Struct stu {int Num; char name [20]; char sex; float score;}; structure stu boy1, boy2;
The two variables BOY1 and BOY2 are STU structural types. It is also possible to define a symbol constant to represent a structural type, for example:
#define stu struct stustu {int Num; char name [20]; char sex; float score;}; stu boy1, boy2;
2. Describe the structural variables while defining structural types. E.g:
Struct stu {int Num; char name [20]; char sex; float score;} BOY1, BOY2;
3. Directly explain the structural variables. E.g:
Struct {int Num; char name [20]; char sex; float score;} BOY1, BOY2;
The third method and the second method are different in the third method, and the structural variable is directly given. When the BOY1, the BOY2 variable is the STU type, you can assign a value to each member of the two variables. In the above STU structure definition, all members are basic data types or array types. Members can also be a structure that constitutes a nested structure. E.g:
Struct Date {int month;} struct {int num; char name [20]; char sex; struct date birthday; float score;} BOY1, BOY2;
First, a structure DATE is defined, consists of three members of Month, Day (Japan), Year (year). When the variable BOY1 and BOY2 are defined, the members BIRTHDAY are illustrated as the DATA structure type. Member names can be interfered with each other in the same name in other variables. The representation of structural variable members often use structural variables in the program, often use it as a whole. In addition to allowing the same type of structural variables to be assigned to each other, including assignment, input, output, calculation, etc., including assignment, input, output, calculation, etc. in addition to the same type of structural variables, including assignment, input, output, calculation, etc.
The general form of the structural variable member is: structural variable name. Member name, such as: boy1.num, the first person's school number BOY2.SEX, the second person's gender, if the member itself is a structure, the minimum must be found step by step Level members can be used. For example: boy1.birdhday.month, the first month of the first person can be used separately in the program, identical to ordinary variables