结构体指针
#include<iostream>
using namespace std;
// 结构体数组
struct student
{
string name;
int age;
int score;
};
int main()
{
// 指向结构体变量
struct student stu = { "张三",18,66 };
struct student* p = &stu;
//指向结构体变量中的数据
// 指针使用箭头
cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
system("pause");
return 0;
}
结构体嵌套
#include<iostream>
using namespace std;
// 结构体嵌套
struct student
{
string name;
int age;
int score;
};
struct teacher
{
string name;
int age;
struct student stu;
};
int main()
{
struct student stu = { "张三",18,66 };
struct teacher wang = { "王老师",40,stu };
cout << "姓名:" << wang.name << " 年龄:" << wang.age << " 辅导学生姓名:" << wang.stu.name <<" 辅导学生成绩"<<wang.stu.score<<endl;
// 或
wang.stu.name = "李四";
wang.stu.age = 19;
wang.stu.score = 100;
cout << "姓名:" << wang.name << " 年龄:" << wang.age << " 辅导学生姓名:" << wang.stu.name << " 辅导学生成绩" << wang.stu.score<<endl;
system("pause");
return 0;
}