1.结构体的概念
结构体就是用户自定义的数据类型
2.结构体定义和使用
语法:struct 结构体名 { 结构体内成员变量 };
//结构体定义
struct Student{ //创建一个学生数据类型
char name; //姓名
int age; //年龄
int score; //分数
};
通过结构体创建变量的方式有三种:
①struct 结构体名 变量名
struct Student stu1;
给变量stu1赋值:
stu1.name="张三";
stu1.age=10;
stu1.score=95;
②struct 结构体 变量名={属性1,属性2,...};
struct Student stu2={"张三",10,95};
③定义结构体时顺便创建变量
struct Student{
string name;
int age;
int score;
}stu3; //stu3就是一个结构体变量
给变量stu3赋值的过程同①中一样