引言:老是忘记这块知识点,于是记录下来。
结构体
同一种数据类型用指针的方式存储,对于不同数据类型但之间有有所关联的元素用结构体就能很好的将其存放在一起。
1、结构体的定义
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include<stdio.h> struct student /*结构体名称*/{ int ID; char name[20]; };
struct student{ int ID; char name[20]; struct teacher; };
|
关于结构体嵌套下,下文有详细讲解,这里不做赘述。
2、结构体的初始化
1 2 3 4 5 6 7 8
| int main() { struct student s1{ "123456789", "张三" }; printf("%d",s1.ID); }
|
定义了结构体,但是系统没有给他分配空间,这里就需要先定义结构体变量
1 2 3 4
| struct stu{ int ID; char name[]; }s1,s2;
|