函数参数传递
来源:互联网 发布:淘宝开店需要什么要求 编辑:程序博客网 时间:2024/06/05 03:06
#include<iostream>using namespace std;struct point{ int x; int y;};void fun1(int a){ a=6;}void fun2(point a){ a.x=7; a.y=8;}int main(){ int a=1; struct point po;//struct可以不写哦 po.x=1; po.y=2; fun1(a); fun2(po); cout<<a<<' '<<po.x<<' '<<po.y<<endl;}
执行结果输出为1 1 2
a=1,po.x=1,po.y=2,函数并未修改这些值。
在fun1()中,相当于临时创建了a,和main里的a不一样,这个函数执行结束后a所占的内存被释放,所以并未修改到main里的a。
改成这样
void fun1(int a){ a=6; cout<<a;}这样会在执行这个函数时打印 6,但在函数外a还是1.
若想修改,需要通过指针来传地址,如下。
#include<iostream>using namespace std;struct point{ int x; int y;};void fun1(int *a){ *a=6;}void fun2(point *a){ (*a).x=7; (*a).y=8;}int main(){ int a=1; point po;//(可以不加struct) po.x=1; po.y=2; fun1(&a); fun2(&po); cout<<a<<' '<<po.x<<' '<<po.y<<endl;}
执行结果为6 7 8
0 0
- 函数参数栈传递
- 函数传递参数
- 函数参数传递
- 函数的参数传递
- 函数的参数传递
- 传递参数给函数
- AS3 函数 传递参数
- 函数参数传递实验
- 函数参数传递->指针
- 函数参数的传递
- 函数参数传递指针
- 函数的参数传递
- 体会函数参数传递。。
- 体会函数参数传递
- 体会函数参数传递
- 体会函数参数传递
- 体会函数参数传递
- 体会函数参数传递
- 2.4jQuery选择器和事件-事件之事件目标与冒泡
- StatefulSet: Kubernetes 中对有状态应用的运行和伸缩
- 赛前训练3.A
- 磁盘检测
- 有道云笔记图片存档
- 函数参数传递
- 文件切割器 合并器
- 面对对象——内部类整理
- PHP获取checkbox值
- Windows安装TensorFlow_v1.0.1
- L1-009. N个数求和(n个分数相加)
- java_project项目相关操作(一)
- C#的protected
- 文件传输工具FileZilla&WinSCP