喜欢你没道理,美丽
对于结构体的定义有点忘了,尤其是typeof,特写此博客来帮助记忆一下
对于C语言结构体定义(不使用typeof)有三种方式:
1.先定义类型再定义变量
struct stu//stu为结构体的名字
{
char name[10];
float score;
};
//该种定义方式如何声明结构体变量
struct stu student1,student2;
2.在定义类型的时候同时定义变量
struct stu
{
char name[10];
float score
}student1,student2;//定义结构体的同时,定义了两个结构体变量student1,student2
//主函数内如何声明结构体变量
struct stu student3;
3.直接定义结构体变量
//该种方式省略了结构体的名称,仅能一次性定义变量,无法在别处定义该结构体类型的变量
struct
{
char name[10];
float score
}student1,student2;
对于C语言结构体定义(使用typeof)
方式1
typedef struct Student
{
int a;
}Stu;
//声明变量,此处的Stu就不是变量了,而是struct Student的别名
Stu stu1;
struct Student stu2;
方式2
typedef struct
{
int a;
}Stu;
//声明变量
Stu stu1;
对于C++结构体的定义
方式1
struct Student
{
int a;
};
//声明变量
Student stu1;
方式2
typedef struct Student
{
int a;
}Stu;//Stu是结构体别名
//声明变量
Stu stu1;
struct Student stu2