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

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

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

b本身就是一个地址,即使函数中是按照值传递,但是这个值就是个地址,效果是一样的

数组首地址就是第一个元素的地址,你这里其实都是地址传递,直接使用数组名当做参数就是传递数组首地址