结构体嵌套结构体
作用:结构体中的成员可以是另一个结构体
例如:每个老师辅导一个学员,一个老师的结构体中记录一个学生的结构体
示例:
#include <iostream>
#include <string>
using namespace std;
//结构体嵌套结构体
//定义一个学生结构体:包括姓名 年龄 分数
struct student {
//成员列表
string name;
int age = 0;
int score = 0;
};
//定义一个老师结构体
struct teacher {
int id = 0;
string name;
int age = 0;
struct student stu;
};
int main(){
//创建老师
struct teacher t;
t.id = 10000;
t.name = "老王";
t.age = 50;
t.stu.name = "小王";
t.stu.age = 20;
t.stu.score = 100;