结构体作为函数参数
#include <iostream>
#include <string.h>
using namespace std;
struct student
{
string name;
int age;
int score;
};
void printStudent1(struct student s)
{
s.age = 199;
s.name = "我哪敢看";
cout << "子函数中 姓名 : " << s.name << " 年龄: " << s.age << " 分数: " << s.score <<endl;
}
void printStudent2(struct student * p)
{
p->age = 600;
p->name ="中";
cout << "子函数中地址传递 姓名 : " << p->name << " 年龄: "
<< p->age << " 分数: " << p->score <<endl << endl;;
}
int main()
{
struct student s;
s.name = "艾米莉";
s.age = 16;
s.score = 100;
printStudent1(s);
cout << "main函数中的打印 姓名: " << s.name << " 年龄: "
<< s.age << " 分数是:" << s.score << endl;
return 0;
}
################结构体中的const 使用场景
#include <iostream>
#include <string.h>
using namespace std;
struct student
{
string name;
int age;
int score;
};
void printStudent(const student *stu)
{
cout << "姓名:" << stu->name << " 年龄: " << stu->age << " 分数: " << stu->score << endl;
}
int main()
{
struct student stu = {"张三",15,70};
printStudent(&stu);
return 0;
}