C语言结构体struct笔记
结构体介绍:
1、结构体:用来存放一组不同类型的数据,在结构体内定义。
2、 结构体定义形式:
struct 结构体名{
结构体包含的数据类型:
例如:
int aaa;
double bbb;
};
3、实例(学生结构体):
struct student{
int id; //学号
int age; //年龄
char name; //名字
float score; //成绩
};
注:student是结构体名字,包含一系列成员变量,不能初始化。
最后的大括号外的“;”不能省略
4、结构体是一种数据类型,因此可以和int等数据类型用相同的引用方式。例如:
struct student stu1, stu2;
其中stu1和stu2是结构体student的两个变量,struct不能省略。
5、定义结构体并直接生成变量:
struct student{ //student可以省略,便不存在结构体名称
int id; //学号
int age; //年龄
char name; //名字
float score; //成绩
} stu1, stu2;
6、赋值:
//变量统一赋值
stu1 = {111, 10, 'ghp', 99.9};
//单个赋值
stu1.id = 10;