C / C ++: passing struct / class with member array to function by value

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 getting 10. I knew that if parameters are passed by value, arrays (also if class / struct members) are passed by reference. It seems, instead, when members, they are passed by copy. Right?

+3


source to share


5 answers


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

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



If you would like to see this modification on the invocation site, follow the non-const link.

+9


source


You pass the variable a

by value and change the content to 30 in the function f

. Since you don't return it or pass it to the function by reference, your change to for a

doesn't affect the value a

in the main function. This is why you are getting 10 and not 30. This is probably what you want:



void f(A &a)
{
  a.V[0]=30;
}

      

+9


source


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

Except that it is not true.

You are confusing things.

An array name, when used directly as a function argument, decays to a pointer to the [first element] of this array. This does not mean that any array, no matter how deeply nested within the encapsulating objects, magically changes its value to "reference".

Indeed, the code you showed is a typical approach to work around this historic name-decay fiasco and get full value semantics for arrays, for example. std::array<T, N>

- that's exactly it.

+3


source


You are actually passing an object of type struct A to the function, and therefore a new hole object will be created, including the Array in your class. so you won't get the result 30. You were fooled by the Array member, if you only passed an array to a function, the value will certainly change, but in your case you are using a struct to wrap the array. as a workaround, you should use a reference or pointer to an A struct as a function parameter.

+1


source


C only has bandwidth. This works the same for all types.

You are confused because "arrays" cannot actually be "passed" in C - in the sense that no function can have an array-type parameter. The C standard states that if you try to write a parameter of type array-of- T

, the compiler will treat it as if you were writing a pointer-to- type T

. Hence, it is not possible to have an array type parameter. When you pass an expression of an array type and the compiler sees that the parameter is expecting a pointer type, it will implicitly convert the array to a pointer to its first element before passing it by value.

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

0


source







All Articles