结构体指针作用:通过指针访问结构体中的成员
语法规则:
利用操作符 -> 可以通过结构体指针访问结构体属性
总结:结构体指针可以通过 -> 操作符来访问结构体中的成员
#include<iostream>
using namespace std;
#include<string>
// 结构体指针
struct Student
{
string name; // 姓名
int age; // 年龄
int score; // 分数
};
int main()
{
// 1. 创建学生结构体变量
struct Student s = { "张三",18,90 };
// 2. 通过指针指向结构体变量
struct Student* p = &s;
// 3. 通过指针访问结构体变量中的数据
// 通过结构体指针 访问结构体中的属性 需要利用 -> 来访问
cout << "姓名:" << p->name << "年龄:" << p->age << "分数:" << p->score << endl;
system("pause");
return 0;
}