#include<iostream>
using namespace std;
struct hero {
string name;
};
void test01(hero *a) { //地址传递
a->name = "王五";
cout << "test01\t函数中:" << a->name << endl;
}
void test02(hero b[]) {//值传递
b[0].name = "阿花";
cout << "test02\t函数中:" << b[0].name << endl;
}
int main(){
hero a[] = {"张三"};
test01(&a[0]);
cout << "test01\tmain函数中" << a[0].name << endl;
hero b[] = { "李四" };
test02(b);
cout << "test02\tmain函数中" << b[0].name << endl;
system("pause");
return 0;
}
数组在传递到函数中,值传递和地址传递有什么区别?我发现好像都能修改当中的东西·
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- lulu最可爱 2022-02-08 11:38关注
这里显然是跟C语言有点像,数组名,就数组的首地址,然后,数组又是连续的地址,故这里也为地址传递,要想看差别,可以传递两个整数看看值传递和地址传递的区别。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 如何获取APP内弹出的网址链接
- ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
- ¥50 STM32单片机传感器读取错误
- ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
- ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
- ¥50 sft下载大文阻塞卡死
- ¥15 机器人轨迹规划相关问题
- ¥15 word样式右侧翻页键消失
- ¥15 springboot+vue 集成keycloak sso到阿里云
- ¥15 win7系统进入桌面过一秒后突然黑屏