1、结构体指针
#include<iostream>
using namespace std;
#include<string> //vs2019不需要添加头文件,其他版本要加
//创建结构体
struct student
{
string name;
int age;
int score;
};
int main()
{
//结构体指针
//创建结构体变量
struct student s1 = { "张三",20,80 };
//创建结构体指针变量
student * p = &s1;//使用int会提示类型错误
//通过指针访问结构体变量
//方法1、结构体变量名.属性
//修改了结构体变量的属性
s1.age = 20;
s1.name = "王五";
s1.score = 100;
cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl;
//方法2、使用->访问结构体变量
cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
system("pause");
return 0;
}
运行结果如下: