天天看點

C++結構體與指針

結構體指針

作用:通過指針通路結構體中的成員

#include <iostream>

#include <string>

using namespace std;

//結構體指針

//定義學生的結構體

struct student

{

       string name;//姓名

       int age;//年齡

       int score;//分數

};

int main()

{

       //建立學生結構體變量

       struct student s={ "張三",18,100 };

       //通過指針指向結構體變量

       struct student *p = &s;

       //通過指針通路結構體變量指哪個的資料

       //通過結構體指針通路屬性需要利用->

       cout << " 姓名:" << p->name

              << " 年齡:" << p->age

              << " 分數:" << p->score << endl;

       return 0;

}