九、结构体
9.1 为什么需要结构体
为了表示一些复杂的事物,普通的基本类型无法满足实际需要。
9.2 什么是结构体
为了模拟一些复杂的事物,把一些基本的数据类型组合在一起,形成了一种新的复合数据类型。
9.3 如何定义结构体(3种方式)
//第一种:经常使用,也是推荐的一种。 struct student1 { int age; floate score; char sex; }; //第二种 struct student2 { int age; floate score; char sex; }st2; //第三种 struct { int age; floate score; char sex; }st3;
9.4 结构体使用
9.4.1 赋值和初始化
int main(void) { struct student1 st1 = {30, 66.6, 'm'}; //方法一:初始化,定义的同时赋值 struct student1 st2; //方法二:先定义结构体变量,然后结构体成员一个一个赋值。 st2.age = 10; st2.sex = 'w'; return 0; }
9.4.2 如何取出结构体变量成员
-
结构体变量名.成员变量名
-
结构体指针变量名->成员变量名