There is actually no semantic analogue for passing an array to a function (by value) as you would pass an integer. This is because array types are not allowed as a function argument.
There is a syntactic analogue: You can indeed write void(int[5])
but the meaning is different. This syntax actually means the same as void(int*)
. In other words, the argument is a pointer.
Another thing I am wondering is the following. Would the arrays int a[5] and int b[6] be considered as having different data types?
int[5]
and int[6]
are different types, yes. However, in a declaration of a function argument, the meaning of the syntax int[5]
is not the type int[5]
but int*
. Likewise, the syntax int[6]
also has the meaning of type int*
. As such, in a declaration of a function argument and only in that context, int[5]
and int[6]
indeed mean the same type: int*
.
An expression of an array type (int[C]
) is automatically converted to pointer to first element (which has type int*
) when passed as an argument. This conversion is called array decaying. Therefore, following well-formed:
void foo(int[5]); // type of argument is int*
void bar() {
int arr[6]; // type of arr is not int*
foo(arr); // arr decays to int*
}
The array is not copied. Array to pointer conversion is performed, and that pointer is copied as the argument. As such, it is possible to modify the objects pointed by the pointer.