前言
本节介绍结构体作为函数参数的用法
一、分类
值传递:形参变,实参不变
地址传递:形参和实参都变了
二、代码示例
1、值传递
1.1 、定义函数
//打印学生信息的函数
//1、值传递
void printStudent1(struct Student s)
{
s.age = 22;
s.score = 95;
cout << "在printStudent1函数中打印的结果:" << endl
<< "姓名:" << s.name << endl
<< "年龄:" << s.age << endl
<< "成绩:" << s.score << endl;
cout << "----------------------------------" << endl;
}
1.2、在主函数中输出对比
//值传递
struct Student s1 = { "小黑",20,99};
cout << "值传递前main函数中打印的结果:" << endl
<< "姓名:" << s1.name << endl
<< "年龄:" << s1.age << endl
<< "成绩:" << s1.score << endl;
cout << "----------------------------------" << endl;
printStudent1(s1);
cout << "值传递后main函数中打印的结果:" << endl
<< "姓名:" << s1.name << endl
<< "年龄:" << s1.age << endl
<< "成绩:" << s1.score << endl;
cout << "----------------------------------" << endl;
1.3、输出结果
由图可知:值传递前后实参不发生变化
2、地址传递
2.1、定义函数
//2、地址传递
void printStudent2(struct Student *p)
{
p->age = 23;
p->score = 97;
cout << "在printStudent2函数中打印的结果:" << endl
<< "姓名:" << p->name << endl
<< "年龄:" << p->age << endl
<< "成绩:" << p->score << endl;
cout << "----------------------------------" << endl;
}
2.2、在主函数中输出对比
//地址传递
struct Student s2 = { "小金",21,98 };
cout << "地址传递前main函数中打印的结果:" << endl
<< "姓名:" << s2.name << endl
<< "年龄:" << s2.age << endl
<< "成绩:" << s2.score << endl;
cout << "----------------------------------" << endl;
Student * p = &s2;
printStudent2(p);
cout << "地址传递后main函数中打印的结果:" << endl
<< "姓名:" << s2.name << endl
<< "年龄:" << s2.age << endl
<< "成绩:" << s2.score << endl;
cout << "----------------------------------" << endl;
2.3、输出结果
总结:由图可知:地址传递前后实参发生变化
3、完整代码
#include<iostream>
#include<string>
using namespace std;
//将结构体做函数参数
//将学生传入一个参数中,打印学生身上所有信息
//值传递:形参变,实参不变
//地址传递:形参和实参都变了
//1、定义结构体
struct Student
{
string name;
int age;
int score;
};
//打印学生信息的函数
//1、值传递
void printStudent1(struct Student s)
{
s.age = 22;
s.score = 95;
cout << "在printStudent1函数中打印的结果:" << endl
<< "姓名:" << s.name << endl
<< "年龄:" << s.age << endl
<< "成绩:" << s.score << endl;
cout << "----------------------------------" << endl;
}
//2、地址传递
void printStudent2(struct Student *p)
{
p->age = 23;
p->score = 97;
cout << "在printStudent2函数中打印的结果:" << endl
<< "姓名:" << p->name << endl
<< "年龄:" << p->age << endl
<< "成绩:" << p->score << endl;
cout << "----------------------------------" << endl;
}
int main()
{
//值传递
struct Student s1 = { "小黑",20,99};
cout << "值传递前main函数中打印的结果:" << endl
<< "姓名:" << s1.name << endl
<< "年龄:" << s1.age << endl
<< "成绩:" << s1.score << endl;
cout << "----------------------------------" << endl;
printStudent1(s1);
cout << "值传递后main函数中打印的结果:" << endl
<< "姓名:" << s1.name << endl
<< "年龄:" << s1.age << endl
<< "成绩:" << s1.score << endl;
cout << "----------------------------------" << endl;
//地址传递
struct Student s2 = { "小金",21,98 };
cout << "地址传递前main函数中打印的结果:" << endl
<< "姓名:" << s2.name << endl
<< "年龄:" << s2.age << endl
<< "成绩:" << s2.score << endl;
cout << "----------------------------------" << endl;
Student * p = &s2;
printStudent2(p);
cout << "地址传递后main函数中打印的结果:" << endl
<< "姓名:" << s2.name << endl
<< "年龄:" << s2.age << endl
<< "成绩:" << s2.score << endl;
cout << "----------------------------------" << endl;
system("pause");
return 0;
}