The concept and definition of structure type

1. Basic overview

Structural type:
A data structure that is neither a basic type nor a pointer, it is a collection of several data of the same or different types. Commonly used construction types are arrays, structures, and unions.
Arrays are used to store multiple data of the same type.
Structs are used to hold multiple different types of data.

2. The concept of structure

A structure is a data structure of a structured type, which is a collection of data of one or more basic types or structured types.

3. Definition of structure type

Define the structure type first, and then define the structure variable

struct 结构体类型名{
    
    
成员列表
};

e.g

struct stu{
    
    
int num;
char name[20];
char sex;
};

Once you have a structure type, you can use the type to define variables.

When defining the structure type, define the structure variable by the way, and you can also define the structure variable later

struct 结构体类型名{
    
    
成员列表;
}结构体变量1,变量2;

e.g

struct stu{
    
    
int num;
char name[20];
char sex;
}lucy,bob,alice;
struct stu stefen, lina;

Note: Generally, the structure type will be defined globally, that is, outside the main function.
Therefore, variables are defined while defining the structure type. These variables are generally global variables
defined after the type is defined. The memory allocation of structure variables depends on the definition. Location.

Definition of unnamed structure

When defining a structure type, there is no structure type name. By the way, structure variables are defined.
Because there is no type name, data of related types cannot be defined in the future.

struct {
    
    
成员列表;
}变量1,变量2

Note: Since the unnamed structure has no structure name, it is impossible to define the structure variable after definition, and the structure variable can only be defined at the same time as the type is defined.
e.g.

struct {
    
    
int num;
char name[20];
char sex;
}lucy,bob;

Aliasing struct types

Usually we rename a structure type and replace the original type with the new type name.

typedef struct 结构体名 {
    
    
成员列表;
}重新定义的结构体类型名A

Note: typedef is mainly used to alias a type. At this time, it is equivalent to giving the current structure a new type name A, which is equivalent to the name of the struct structure. Therefore, if the structure needs to be aliased, it is generally not necessary to give the structure a new name. The structure defines the name. When defining the structure variable, just use A directly without adding struct.
e.g.

typedef struct stu{
    
    
	int num;
	char name[20];
	char sex;
}STU;

In the future, STU is equivalent to struct stu. STU lucy; and struct stu lucy; are equivalent, so the name stu may not be specified.

Guess you like

Origin blog.csdn.net/shuting7/article/details/130274197