C++基礎之結構體指針

結構體指針:通過指針訪問結構體中的成員。

具體實現:

#include <iostream>
using namespace std;
#include<string>

/*
	定義一個結構體
*/
struct Student
{
	string name;
	int age;
	int score;
};

int main() 
{
	/*
		創建結構體變量
	*/
	struct Student s1 = {"小紅",12,68};

	/*
		指針指向結構體變量
	*/
	struct Student *p = &s1;

	/*
		指針訪問結構體變量中的數據
	*/
	cout << p->age << p->name << p->score << endl;

	system("pause");
	return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章