# 8.7结构体中const用法
#include <iostream>
using namespace std;
#include <string>
struct student
{
string name;
int age;
int score;
};
//将函数中形参改为指针可以减少内存空间,
//不会复制一个新的副本
void printstudent(const student *s)
{
//s->score = 444;加const后,就不能在修改了!!
cout << s->age << endl;
}
int main8_7()
{
struct student s = { "aaa", 15, 70 };
printstudent(&s);
system("pause");
return 0;
}
# 8.6结构体做函数参数
#include <iostream>
using namespace std;
#include <string>
struct student
{
string name;
int age;
int score;
};
//值传递方法
void printst(struct student ss)
{
cout << "值"<<ss.name << endl;
}
//地址传递方法
void printst2(struct student *p)
{
cout << p->age;
}
int main8_6()
{
struct student s;
s.age = 44;
s.name = "sss";
s.score = 88;
printst(s);
printst2(&s);
//cout << "main" << s.name << endl;
system("pause");
return 0;
}