#include<iostream>
#include<string>
using namespace std;
//如果不想修改主函数中的数据,用值传递,反之用地址传递
//定义结构体
struct Student {
string name;
int age;
int score;
};
//值传递
void printstu1(struct Student s) {
cout << "学生姓名: " << s.name << "年龄: " << s.age << "成绩: " << s.score << endl;
}
//地址传递
void printstu2(struct Student* p) {
cout << "学生姓名: " << p->name << "年龄: " << p->age << "成绩: " << p->score << endl;
}
int main() {
struct Student s1;
s1.name = "jack";
s1.age = 12;
s1.score = 67;
cout << "学生姓名: " << s1.name << "年龄: " << s1.age << "成绩: " << s1.score << endl;
//值传递
printstu1(s1);
//地址传递
printstu2(&s1);
system("pause");
return 0;
}
结构体做函数参数
最新推荐文章于 2024-10-16 10:13:45 发布
本文介绍了C++中如何通过值传递和地址传递操作结构体Student,展示了printstu1和printstu2函数的使用,并在main函数中实例化了一个Student对象并进行传递。
摘要由CSDN通过智能技术生成