作用:将结构体作为参数向函数中传递
传递方式有两种:
*值传递 :无法改变实参
*地址传递 :可以改变实参
实例:
#include<iostream>
using namespace std;
#include<string>
//定义学生结构体
struct student
{
//姓名
string name;
//年龄
int age;
//分数
int score;
};
//打印学生信息函数
//1.值传递
//void printStudent1(struct student s)
//{
// cout << "子函数1中 姓名: " << s.name
// << " 年龄 " << s.age
// << " 分数 " << s.score << endl;
//}
//2.地址传递
void printStudent2(struct student* p)
//表示该函数接受指向struct student类型对象的指针
{
cout << "子函数2中 姓名: " << p->name
<< " 年龄 " << p->age
<< " 分数 " << p->score << endl;
}
int main()
{
//结构体做函数参数
//将学生传入到一个参数中,打印学生身上的所有的信息
//创建结构体变量
struct student s;
s.name = "张三";
s.age = 20;
s.score = 85;
//printStudent1(s);
printStudent2(&s);//使用了&运算符来获取对象s的地址,
/*cout << "main函数中打印 姓名: " << s.name
<< " 年龄 " << s.age
<< " 分数 " << s.score << endl;*/
system("pause");
return 0;
}
仅个人看视频笔记与理解,如有误可指出谢谢