qqqqkkkk001 2022-02-08 11:30 采纳率: 57.1%
浏览 36
已结题

数组在传递到函数中,值传递和地址传递有什么区别?我发现好像都能修改当中的东西·

#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;
}

  • 写回答

3条回答 默认 最新

  • lulu最可爱 2022-02-08 11:38
    关注

    这里显然是跟C语言有点像,数组名,就数组的首地址,然后,数组又是连续的地址,故这里也为地址传递,要想看差别,可以传递两个整数看看值传递和地址传递的区别。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 2月16日
  • 已采纳回答 2月8日
  • 创建了问题 2月8日