#include<iostream>
using namespace std;
#include<ctime>
#include<string>
struct student {
string name;
int age;
int score;
};
void printstudent(struct student s) {
cout << "子函数中 姓名" << s.name << "年龄" << s.age << "分数" << s.score << endl;
}
void printstudent2(struct student* p) {
p->age = 100;//非常重要 值传递中改变变量 总体也要跟着改变
p->name = "李四";
p->score = 100;
cout << p->name << p->age << p->score << endl;
}
int main() {
struct student s;
s.name = "张三";
s.age = 20;
s.score = 85;
cout << s.name << s.age << s.score << endl;
//printstudent(s);
printstudent2(&s);
system("pause");
return 0;
}
二次练习
#include<iostream>
using namespace std;
#include<ctime>
#include<string>
struct student { //这一行不能定义stu
string name;
int score;
int age;
};
//void printStudent1(struct student s) {
// s1.score = 100;
// s1.name = "明天";
// s1.age =18;
// cout << s1.name << s1.age<<s1.score << endl;
//}//值传递
void printStudent2(struct student *p) {
p->name = "梨花";
p->age = 18;
p->score = 89;
cout << p->name << p->age << p->score << endl;
}
int main() {
struct student s1;
s1.name = "黎明";
s1.score = 70;
s1.age = 66;
cout << s1.name << s1.age << s1.score << endl;
//printStudent1(s1);//函数声明时候不需要函数类型
printStudent2(&s1);
system("pause");
return 0;
}