結構體 — 結構體作函數參數

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

注意:ios

若是不想修改主函數中的數據,用值傳遞,反之用地址傳遞ide

點擊查看代碼
#include<iostream>
#include<string> 

using namespace std;


struct student
{
	//成員列表
	string name;

	int age;

	int score;

};

//打印學生信息的函數
//值傳遞
void printStudent1(struct student s)
{
	cout << "子函數1 中學生的姓名:" << s.name << endl;
}

//地址傳遞
void printStudent2(struct student *p)
{
	cout << "子函數2 中學生的姓名:" << p->name << endl;
}

int main(){
	
	//結構體作函數參數
	//將學生傳入到一個參數中,打印學生身上的全部信息

	//建立結構體變量
	student s;
	s.name = "李明";

	printStudent1(s);
	printStudent2(&s);
	cout << "main函數中學生的姓名:" << s.name << endl;

	system("pause");

	return 0;
}
相關文章
相關標籤/搜索