C++结构体指针

473 阅读1分钟
#include<iostream>
using namespace std;
#include <string>

/*
结构体指针
作用:通过指针访问结构体中的成员
利用操作符->可以通过结构体指针访问结构体属性
*/

//1. 创建学生数据类型 
struct  Student
{
    //姓名
    string name;
    //年龄
    int age;
    //分数
    int score;
};


int main(){
    
    //1. 创建学生结构体变量
    struct Student s = {"peter",22,99};
    //2. 通过指针指向结构体变量
    struct  Student * p = &s;
    //3. 通过指针访问结构体变量中的数据
    cout << "姓名" << p->name << "年龄" << p->age << "分数" << p->score << endl; 
    system("pause");
    return 0;
}