Struct, shared body, and user-defined type (1), shared definition
Basic Types of C language: INTEGER (int), char, float, double, void) and the derived type (pointer and array ). This chapter describes three data types that can be constructed by users in C:
(1) User-Defined type (typedef); (2) struct (struct); (3) union );
1. Use typedef to specify the new type name
The C language allows typedef to indicate a new type name. The general format is:
Typedef type name identifier;
The "type name" must be a type identifier that has been defined before this statement. "Identifier" is a user-defined identifier. The typedef statement only uses "identifiers" to represent existing "type names" and does not generate new data types. The original type names are still valid. For example:
Typedef int INTEGER;
This statement refers to an int type name as an identifier INTEGER. After this description, you can use the identifier INTEGER to define the INTEGER variable. For example:
INTEGER m, n; equivalent to int m, n;
INTEGER is an alias of int. For easy identification, it is generally used to indicate the new type name with uppercase letters. For example:
Typedef char * CHARP;
CHARP p;
Equivalent to: char * p;
2. Structure Type 2.1 description of struct type
The structure type description is generally in the following format:
Struct Identification name
{
Type name 1 structure member table 1
Type name 2 structure member table 2
...
Type name n structure member table n
};
Struct is the keyword. The "struct Identification name" and "schema member name" are user-defined identifiers. The "struct Identification name" is optional and does not appear in the description. Each "structure member table" can contain multiple names of the same type, which are separated by commas. The member names in the struct can have the same name as other variables in the program. members in different structs can also have the same name. Note: The structure description must end with a semicolon.
Struct date
{Int year, month, day ;};
The "type name" in the struct type description can be either a simple data type or a struct type. When the struct description contains the struct, the struct is nested. As follows:
Struct student
{
Char name [12];
Char sex;
Struct date birthday;
Float SC [4];
};
The ansi c standard specifies that a struct should be nested at least 15 layers, and that the names of embedded struct members should be the same as those of outer members.
The description of the struct type only lists the composition of the structure. It indicates that this type of structure "pattern" already exists and the Compilation Program does not allocate any storage space. The variables, arrays, and dynamic storage units with corresponding struct types occupy the storage space. Therefore, before using struct variables, arrays, or pointer variables, you must first define these variables, arrays, or pointer variables.