码迷,mamicode.com
首页 > 其他好文 > 详细

结构体嵌套,指针形参,引用传参

时间:2019-12-07 10:30:10      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:pause   嵌套   修改   hang   win   phone   clu   name   teacher   

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 
 6 struct Student
 7 {
 8     int s_id;
 9     string s_name;
10     int s_phonenum;
11 };
12 
13 struct Teacher
14 {
15     int m_id;
16     string m_name;
17     Student m_stu;//班长
18     Student starr[50];//班级学生
19 };
20 void showinfo1(const Teacher *ter)//指针形参,加了const修饰指针,保证该函数不修改实参的值
21 {
22     cout << "老师姓名: " << ter->m_name << endl;
23     cout << "班级1号学生的电话: " << ter->starr[0].s_phonenum << endl;
24 }
25 
26 void showinfo2( Teacher &ter)//引用传参
27 {
28     cout << "老师姓名: " << ter.m_name << endl;
29     cout << "班级1号学生的电话: " << ter.starr[0].s_phonenum << endl;
30 }
31 
32 int main()
33 {
34     Student stu = { 1,"zhangsan",1467888 };
35     Student syuarr[3] =
36     {
37         {2,"2hao同学",12222222},
38         {3,"3#tongxue",1333333},
39         {4,"4#tongxue",1444443}
40     };
41 
42     Teacher ter;
43     ter.m_id = 12;
44     ter.m_name = "wang";
45     ter.m_stu = stu;
46     ter.starr[0] = syuarr[1];
47 
48     showinfo1(&ter);
49     showinfo2(ter);
50 
51     system("pause");
52     return 0;
53 }

 

结构体嵌套,指针形参,引用传参

标签:pause   嵌套   修改   hang   win   phone   clu   name   teacher   

原文地址:https://www.cnblogs.com/rtblogs/p/12000733.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!