Alcott Alcott - 7 months ago 45
C++ Question

Why can't we pass arrays to function by value?

Apparently, we can pass complex class instances to functions, but why can't we pass arrays to functions?


The origin is historical. The problem is that the rule "arrays decay into pointers, when passed to a function" is simple.

Copying arrays would be kind of complicated and not very clear, since the behavior would change for different parameters and different function declarations.

Note that you can still do an indirect pass by value:

struct A { int arr[2]; };
void func(struct A);