First of all this function declaration
void func (int x[2][2])
{
x[1][2] = 5;
}
is equivalent to
void func (int x[][2])
{
x[1][2] = 5;
}
and in turn is equivalent to
void func (int ( *x )[2])
{
x[1][2] = 5;
}
that is the parameter has type of pointer to one-dimensional array of type int[2]
. The function has gotten an address of a memory.
When you call the function the following way
func (a);
array a
is implicitly converted to pointer to its first "row" (first element).
So the function deals with a pointer. It does not change the pointer itself.
It changes thye memory pointed to by this pointer.
Within the function body expression expression
x[1]
is equivalent to *( a + 1 )
and yields the second "row" of the array a (indices starts from 0).Let's name it row
Expression
x[1][2]
is equivalent to row[2]
and yields reference to the third element of this row.
The value in this cell of the memory occupied by the array is changed in the function. That is the function does not deal with a copy of the value in this cell. It deals directly with the cell itself because we provided its address.
Take into acccount that this statement
cout << a[1][2] << endl;
is wrong. The valid range of indices for array a
declared like
int a[2][2]
is [0, 1]
.
So you are trying to override memory beyond the array.