2. Structure (struct)
A structure is a combination of various variables that are composed of basic data types and named with an identifier. Different data types can be used in the structure.
1. Structure description and Structure variable definition
In Turbo C, a struct is also a data type, and you can use a struct variable, so, as with other types of variables, you define the structure variables first.
The general format for defining structure variables is:
struct structure name
{
The type variable name;
The type variable name;
...
} structure variables;
The structure name is the identifier of the struct, not the variable name.
Type is the five data types described in section II (integer, floating-point, character, pointer, and no value).
Each type variable of the constituent structure is called a struct member, and it is like an element of an array, but the elements in the array are accessed by the following object, and the struct accesses the member by the variable name.
Here's an example to illustrate how to define a struct variable.
struct string
{
Char Name[8];
int age;
Char sex[2];
Char depart[20];
Float Wage1, Wage2, Wage3, Wage4, Wage5;
} person;
This example defines a struct variable person with a struct named string, and if you omit the variable name person, it becomes a description of the structure. Structure variables can also be defined with the described structure name. The example above in this definition becomes:
struct string
{
Char Name[8];
int age;
Char sex[2];
Char depart[20];
Float Wage1, Wage2, Wage3, Wage4, Wage5;
};
struct string person;
If you need to define more than one structure variable with the same form, it is more convenient to use this method, it is a structure description, and then use the structure name to define the variable.
For example:
struct string tianyr, Liuqi, ...;
If the structure name is omitted, it is called the nameless structure, which often occurs inside the function, and the previous example in this structure becomes:
struct
{
Char Name[8];
int age;
Char sex[2];
Char depart[20];
Float Wage1, Wage2, Wage3, Wage4, Wage5;
} Tianyr, Liuqi;