作用:结构体中的成员可以是另一个结构体
例如:每个老师辅导一个成员,一个老师的结构体中,记录另一个学生的结构体
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 //定义学生结构体 6 struct Student 7 { 8 string name; 9 int age; 10 int score; 11 }; 12 13 //定义老师结构体 14 struct Teacher 15 { 16 string name; 17 int id; 18 int age; 19 20 struct Student stu;//创建结构体变量 21 }; 22 23 24 int main(void) 25 { 26 //老师结构体初始化 27 struct Teacher t; 28 t.name = "小李"; 29 t.id = 10000; 30 t.age = 22; 31 t.stu.name = "张三"; 32 t.stu.age = 18; 33 t.stu.score = 60; 34 35 cout << "老师:"<< t.name << " ID:" << t.id << " 年龄" << t.age << endl; 36 cout << "学生:" << t.stu.name << " 年龄:" << t.stu.age << " 分数:" << t.stu.score << endl; 37 38 system("pause"); 39 return 0; 40 }