+ 1

Are arrays ALWAYS passed by Reference?

In this example: void f(int myArr[],int sz){ for(int i=0;i<sz;i++) myArr[i]=i+1; } int main(){ int arr[3]={68,52,47}; f(arr,3); for(int i=0;i<3;i++) cout<<myArr[i]; } The output is: 123

23rd Apr 2017, 11:44 PM
Guerreiro
1 Réponse
+ 8
Yes, c-style arrays are always passed by reference. However, if you use the std::array class introduced in C++11 (declared in the <array> header), you have more control over how arrays are passed: void func1(std::array<int, 10> arr) { // array is passed by value } void func2(std::array<int, 10>& arr) { // array is passed by reference }
24th Apr 2017, 12:05 AM
Squidy
Squidy - avatar