结构体
1.正常定义的结构体。
struct Student{ //声明结构体
char name[5]; //姓名
int num; //学号
int score; //成绩
};
struct Student stu1; //定义结构体变量
2.在声明结构体的同时声明变量。
struct Student{
char name[5];
int num;
int score;
}stu1,stu2;
//这种声明方式等同于下面这段代码。
struct Student{
char name[5];
int num;
int score;
};
struct Student stu1,stu2;
3.声明一个匿名结构体并创建变量。
struct {
char name[5];
int num;
int score;
}stu1,stu2;
使用这种方式声明的结构体是匿名的,只能在声明结构体的同时创建变量。
typedef修饰的结构体
typedef的作用是为某种数据类型取==别名==。
1.typedef 修饰的结构体函数
typedef struct Student {
char name[5];
int num;
int score;
}Stu; //声明struct Student的别名,Stu。
//此时以下两个式子等价。
struct Student stu1;
Stu stu1;
2.typedef修饰的匿名结构体函数。
typedef struct{
char name[5];
int num;
int score;
}Stu,Stud; //声明两个别名。
//虽然我们没有办法使用结构体的名字声明变量。但可以通过Stu与Stud声明变量。
Stu stu1;
Stud stu2; //不管是使用Stu还是Stud声明,效果都是一样的。