一.结构体是什么
不同类型的多个变量放在一起的集合 (数组为相同类型)
二.结构体是什么
1.定义结构体
struct 结构体名{
成员列表;
};
例:定义一个学生的结构体
struct Student {
char name[32]; //姓名
int age; //年龄
float score; //成绩
};
2.定义结构体变量
struct Student student1;
就定义了一个Student
类型的结构体变量student1
3.结构体成员引用
结构体变量名.成员名
例:student1.age
4.结构体赋值
例:student2 = student1;
结构体能整体赋值,数组不能。
5.结构体整体初始化
例: struct Student student1 = {"张三",19,90.5};
6.结构体部分初始化
例: struct Student student1 = {.name="张三"};
三.其他语法
1定义结构体并同时定义结构体变量
struct Student{
char name[32]; //姓名
int age; //年龄
float score; //成绩
} student1,student2;
2 定义结构体并同时定义结构体变量并赋初值
struct Student{
char name[32]; //姓名
int age; //年龄
float score; //成绩
} student1 = {“Zhang”, 19, 90.5};