1.先定义结构体类型,再定义结构体变量
struct student{
int age;
char* name;
};
struct student stu;//stu为student的结构体变量
2.定义结构体变量的同时,定义结构体变量
struct student{
int age;
char* name;
}stu;
//如果想要继续定义结构体变量:
struct student stu1;//这样既可以再次定义结构体变量
3.不定义结构体类型,而直接定义结构体变量
struct{
int age;
char* name;
}stu;
//这样的话我们就不能再次定义stu该类型的结构体变量了,
//可移植性非常差,也不灵活。
4.用typedef 来定义结构体变量及类型 (推荐)
typedef sturct student{
int age;
char* name;
}Stu;
这样定义的话,结构体类型的名字就有了两个分别为student和Stu
定义结构体变量的方式也就有了两种
struct student s1;//方式1
Stu s2;//方式2