C++ structure type variable

C++ method of defining structure type variable

1. Declare the structure type first and then define the variable name. After the structure variable is defined, the system will allocate a memory unit for it.

struct Student{
    
     //自定义结构体变量
    int num;//学号
    char sex;//性别
    int age;//年龄
  };

2. Define variables while declaring the type

The general form is

struct 结构体名
{
    
    
     成员表列
}变量名表列;
struct Student{
    
     //自定义结构体变量
    int num;//学号
    char sex;//性别
    int age;//年龄
  }student1,student2;

3. Directly define structure type variables (rarely used in development, and Xiaolin does not recommend it)

The general form is

struct
{
    
    
    成员表列
}变量名表列;
struct {
    
     //自定义结构体变量
    int num;//学号
    char sex;//性别
    int age;//年龄
  }student1,student2;

Regarding the structure type, there are the following 5 points for readers' attention:

Don't mistakenly think that all structure types have the same structure. Each structure type has its own structure, and many specific structure types can be defined.

Types and variables are different concepts. You can only assign values ​​to members in structure variables, but not to structure types. At compile time, no space is allocated for types, only space is allocated for variables.

The members in the structure can be used alone, which is equivalent to ordinary variables.

The member can also be a structure variable.

The name of the member in the structure can be the same as the name of the variable in the program, but the two are not related.

C++ structure type variable

More cases can go to the public account: C language entry to mastery

Guess you like

Origin blog.csdn.net/weixin_48669767/article/details/111841845