#include<iostream>
using namespace std;
#include<string>
//结构体指针
//定义结构体
struct student
{
//成员列表
//姓名
string name;
//年龄
int age;
//分数
int score;
};
int main()
{
//1.创建学生结构体变量
struct student s1 = { "张三",18,99 };
//2.通过指针指向结构体变量
struct student *p = &s1;
//3.通过指针访问结构体变量中的数据,使用操作符 -> 箭头指向结构体指针中的具体内容
cout << "姓名:" << p->name << " " << "年龄:" << p->age << " " << "分数:" << p->score << endl;
cout << endl;//与下面的p2数组做划分,换行
//尝试用指针访问结构体数组,并且遍历打印
struct student stuArr[3]=
{
{"马萨",18,97},
{"李琦",19,98},
{"向奇",20,99}
};
struct student *p2 = stuArr;
//遍历循环数组打印
for (int i = 0; i < 3; i++)
{
cout << "姓名:" << p2->name << " " << "年龄:" << p2->age << " " << "分数:" << p2->score << endl;
p2++;//移动到下一行,进行打印,放在输出后面,是因为要先打印第1行,stuArr[0]
}
system("pause");
return 0;
}