结构体作为参数传入函数错误
2016-03-23 19:24
267 查看
结构体作为参数传递的错误!
如下代码:定义了一个结构体变量,将该结构体变量作为参数传递给函数func2,此时传递给函数func2的是结构体变量my_sis的拷贝,函数func2中对所传入的结构体的修改不影响main函数中定义的结构体,所修改的只是传入的拷贝的副本。
1#include <stdio.h>
2
3 struct score
4 {
5 int math;
6 int english;
7 };
8
9 struct stu
10 {
11 int age;
12 struct score core;
13 };
14
15 void fun(int b[5])
16 {
17 b[3]=8;
18 }
19 void fun2(struct stu my)
20 {
21 my.core.math = 100;
22 }
23
24 int main()
25 {
26 //int a[5]={1,2,3,4,5};
27 struct stu my_sis;
28
29 my_sis.core.math = 10;
30 my_sis.core.english = 50;
31
32 /*
33 fun(a);
34 printf("%d\n",a[3]);
35 */
36 fun2(my_sis);
37 printf("%d\n",my_sis.core.math);
38
39 }
如下代码:定义了一个结构体变量,将该结构体变量作为参数传递给函数func2,此时传递给函数func2的是结构体变量my_sis的拷贝,函数func2中对所传入的结构体的修改不影响main函数中定义的结构体,所修改的只是传入的拷贝的副本。
1#include <stdio.h>
2
3 struct score
4 {
5 int math;
6 int english;
7 };
8
9 struct stu
10 {
11 int age;
12 struct score core;
13 };
14
15 void fun(int b[5])
16 {
17 b[3]=8;
18 }
19 void fun2(struct stu my)
20 {
21 my.core.math = 100;
22 }
23
24 int main()
25 {
26 //int a[5]={1,2,3,4,5};
27 struct stu my_sis;
28
29 my_sis.core.math = 10;
30 my_sis.core.english = 50;
31
32 /*
33 fun(a);
34 printf("%d\n",a[3]);
35 */
36 fun2(my_sis);
37 printf("%d\n",my_sis.core.math);
38
39 }
相关文章推荐
- ExpandableListView(可展开的列表组件)的说明以及其用法
- OC对象的内存销毁步骤
- iOS开发:OC篇:常用字符串方法的运用
- Spring常用jar包的功能
- 条件查询N多的情况下,回显解决方法。
- 03Linux文件基本操作管理
- oracle merge into用法
- [LeetCode] Shortest Word Distance I & II & III (好题)
- eclipse中高亮显示选中的所有对象
- 工厂模式
- a标签绑定点击事件
- python问题:IndentationError:expected an indented block错误解决
- javascript理解js闭包
- 单元测试练习
- 作业三(1)
- uva10859 Placing Lampposts (树形dp+求两者最小值方法)
- 通过模板函数获取数组长度深入探讨
- Problem f
- ajax实现jsp页面的动态刷新时间
- C++ 穷举法 三色球问题