3
struct A{
  int V[100];
};


void f(A a)
{

  a.V[0]=30;
}

int main()
{
  A a;
  a.V[0]=10;
  f(a);
  cout<<a.V[0];
}

I expected 30 as output, instead I obtain 10. I knew that, also if the parameters are passed by value, arrays (also if members of class/struct) are passed by reference. It seems instead, when members, they are passed by copy. Is true?

Lundin
  • 195,001
  • 40
  • 254
  • 396
volperossa
  • 1,339
  • 20
  • 33

5 Answers5

9

You pass the variable a by value and change the content to 30 in the function f. Since you don't return it back and don't pass it to the function by reference, your change for a doesn't affect the value of a in the main-function. That is the reason why you get 10 and not 30. This is probably what you want:

void f(A &a)
{
  a.V[0]=30;
}
Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
mbed_dev
  • 1,450
  • 16
  • 33
9

Passing an array by value to a function as an argument causes it to decay to a pointer to the first element, so is like passing by reference.

Passing an object containing an array (not a pointer) by value to a function causes that object, including the array, to be copied into the function's parameter.

If you want to see that modification at the call site, pass by non-const reference.

TartanLlama
  • 63,752
  • 13
  • 157
  • 193
3

I knew that, also if the parameters are passed by value, arrays (also if members of class/struct) are passed by reference.

Except that this is not true.

You're confusing things.

The name of an array, when used directly as a function argument, decays to a pointer to [the first element of] that array. That does not mean that any array, no matter how far nested in encapsulating objects, magically changes from a value into a "reference".

Indeed, the code you've shown is the typical approach to workaround this historical array-name-decay fiasco and get full value semantics for arrays, e.g. std::array<T, N> is precisely this.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

Actually you're passing a object of type struct A to the function and therefore a hole new object will be created including the Array in you class . so you won't get 30 as result.you were mislead by the Array member if you were passing only an array to the function the value will surely be changed but in your case you using a struct to wrap your array. as a workaround you should use reference or pointer on A struct as a function parameter.

Anis Belaid
  • 304
  • 2
  • 8
0

C only has pass-by-value. This works the same way for all types.

You are confused because "arrays" actually cannot be "passed" in C -- in the sense that no function can have a parameter of array type. The C standard specifies that if you try to write a parameter of array-of-T type, the compiler will treat it as if you wrote pointer-to-T type. Therefore, it is impossible to have a parameter of array type. When you pass an expression of array type, and the compiler sees that the parameter expects a pointer type, it will implicitly convert the array into a pointer to its first element, before passing it by value.

On the other hand, it is possible to have a parameter of struct type.

newacct
  • 119,665
  • 29
  • 163
  • 224