形参为:const student *s是指针传递
形参为:const student s值传递(复制一份)
为了避免地址传递时值被修改,可以加const误操作。
#include <iostream>
#include "string"
using namespace std;
//定义学生结构体
struct student {
//成员列表
string name; //姓名
int age; //年龄
};
void printStudent(const student *s) {
//s->age = 100;//语法错误,被const修饰的形参不能被修改
cout << "姓名:" << s->name << "年龄:" << s->age << endl;;
}
int main() {
//创建结构体变量
struct student s1 = {"张三", 18};
printStudent(&s1);
}