1. 程式人生 > 其它 >const修飾結構體指標

const修飾結構體指標

技術標籤:C++筆記c++

const修飾結構體指標

#include<iostream>
using namespace std;
#include<string>
//定義一個結構體
struct student
{
	string name;
	int age;
	int score;
};

void printfStudent( const struct student* q1)
//在函式中將形參改為指標,這樣可以減少記憶體,不會複製多的副本
//const 結構體指標可以避免誤修改主函式實參
{
	
	cout << "子函式中 姓名  "
<< q1->name << " 年齡 " << q1->age << " 分數 " << q1->score << endl; }; int main() { //定義一個結構體變數 student q1; q1.name = "WUNGF"; q1.age = 21; q1.score = 90; printfStudent(&q1); cout << "主函式 姓名 " <<
q1.name << " 年齡 " << q1.age << " 分數 " << q1.score << endl; system("pause"); return 0; }

在這裡插入圖片描述