認識 結構體指針
利用操作符 -> 可以通過結構體指針通路結構體屬性
#include<iostream>
#include<string>
using namespace std;
struct student
{
stringname ;
intage ;
intscore ;
};//建立結構體
int main()
student zs = { "張三",21,85 };//
建立和定義結構體變量
//通過指針指向結構體變量(建立結構體指針)
structstudent* p = &zs;
cout << "姓名:" <<zs.name <<endl<< "
年齡:" << zs.age <<endl<< "分數"<< zs.score << endl;
//利用指針通路結構體變量
p->age= 58;
p->name = "劉易斯";
p->score= 99;
cout << "姓名:" <<p->name << endl << "
年齡:" << p->age << endl <<"
分數" << p->score << endl;
}
總結 :
一般的結構體通路用操作符 . 通路(struct student zs.name)