In the previous course we learned some simple data types (integer, the definition and application of the real type, character type, and the definition and application of array (one-dimensional, two-dimensional), which is characterized by defining the storage attribute and range of the type variable when a particular data type is defined. For simple data types, you can define a single variable,
You can also define an array. All elements of an array have the same data type, or a collection of the same data type.
In daily life, we often encounter some need to fill in the registration form, such as accommodation table, score table, mailing address.
In these tables, the data can not be described in the same data type, in the accommodation table we will usually register the name, gender, ID number and other items; In the mailing Address table we will write the name, postcode, email address, telephone number, e-m a i l and other items. Each of these tables is a collection of data that cannot be fully described with any of the data types previously learned, so C introduces a type of data-struct type that can centralize different data types in one. A variable of a struct type can have members of different data types and is a collection of members of different data types.
In the various forms described above, let's take a closer look at the accommodation table, the score sheet, the mailing address, and so on.
The accommodation table is comprised of the following items:
These registration forms are described in the type of structure provided by C as follows:
Accommodation table:
struct ACCOMMOD
{
Char name[20]; /* Name */
char sex; /* Gender */
Char job[40]; /* Occupation */
int age; /* Age */
Long number; /* ID Card number */
} ;
Score Sheet:
struct SCORE
{
Char grade[20]; /* Class */
Long number; /* School Number */
Char name[20]; /* Name */
float OS; /* Operating system */
float Datastru; /* Data structure */
float compnet; /* Computer network */
} ;
Mailing Address Table:
struct ADDR
{
Char name[20];
Char department[30];/* Department */
Char address[30]; /* Address */
Long box; /* ZIP code */
Long phone; /* Telephone Number */
Char email[30]; /* E m a i l */
};
This series of descriptive types for the data structures of the different registration forms is called the structure body type. Because different problems have different data members, that is to say, there are different types of structures that are described. We can also understand that the struct type is different from its members according to the problem being addressed and can have any number of structural body types described.
The following defines the form of C to struct type:
struct Structural body Name
{
Member Item table columns
};
With struct type, we can define struct type variables to refer to members of different variables.
Definition of 7.1.1 type variable of structure body
The definition of a struct type variable is the same as that of other types of variables, but because the struct type needs to be defined in advance, the definition of the type variable of the struct body increases the flexibility by a total of three forms, each of which is described as follows:
1 Define the structure body type and then define the structural body type variables:
struct STU/* Define student structure type */
{
Char name[20]; /* Student Name */
char sex; /* Gender */
Long num; /* School Number */
float score[3]; /* Test results for three subjects */
};
struct Stu student1,student2;/* define struct type variable */
struct Stu Student3,student4;
With this struct type, you can define more of the struct type variable.
2 Define the structure body type and define the struct type variable:
struct data
{
int day;
int month;
int year;
} time1,time2;
You can also define the following variables:
struct data time3,time4;
With this struct type, you can also define more of the struct type variable.
3) Direct definition of struct type variables:
struct
{
Char name[20]; /* Student Name */
char sex; /* Gender */
Long num; /* School Number */
float score[3]; /* Test results for three subjects */
} Person1,person2; /* Define the structure body type variable */
This definition method cannot be defined as a struct type variable, except for direct definition, because the struct body type cannot be logged.