#include<iostream>
#include<string>
using namespace std;
/*
结构体中const的使用场景
用const来防止误操作
*/
struct student{
string name;
int age;
int score;
};
void print_stu_1(student s){ // 形参s,~=实参s的复制
cout << "姓名:" << s.name << ",年龄:" << s.age << ",分数:" << s.score << endl;
}
void print_stu_2(const student * p){ // 将函数中形参改为指针,可以节省内存空间,且不会拷贝新副本
//p->score = 150; // 形参指针前加上const后该句报错,防止误改。仅可读取不可修改
cout << "姓名:" << p->name << ",年龄:" << p->age << ",分数:" << p->score << endl;
}
int main(){
struct student s = {"李四", 19, 80}; // 实参s
cout << "姓名:" << s.name << ",年龄:" << s.age << ",分数:" << s.score << endl;
print_stu_1(s);
print_stu_2(&s);
return 0;
}