#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语言有点像,数组名,就数组的首地址,然后,数组又是连续的地址,故这里也为地址传递,要想看差别,可以传递两个整数看看值传递和地址传递的区别。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报