-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path07_struct_03.cpp
61 lines (50 loc) · 1.02 KB
/
07_struct_03.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
using namespace std;
// 结构体嵌套结构体
struct student
{
string name;
int age;
int score;
};
struct teacher
{
int id;
string name;
struct student stu; // 子结构体,学生
};
// 结构体做函数参数
// 值传递
void printStudent1(student stu)
{
stu.age = 21;
cout << "printStudent1" << stu.name << stu.age << stu.score << endl;
}
// 地址传递
void printStudent2(struct student *p)
{
p->age = 25;
cout << "printStudent1" << p->name << endl;
}
int main()
{
// 结构体嵌套
teacher t;
t.id = 10086;
t.name = "老王";
t.stu.name = "张三";
t.stu.age = 20;
t.stu.score = 60;
// cout << t.name << endl;
// cout << t.stu.name << endl;
// 结构体做函数参数
struct student s;
s.name = "李四";
s.age = 20;
s.score = 70;
printStudent1(s);
cout << s.age << endl; // 仍为20,修改形参不影响实参
printStudent2(&s);
cout << s.age << endl; // 已改为25
return 0;
}