I was just wondering why it is allowed to omit the leftmost index of a multidimensional array when passing the array to a function ? Why not more than one indexes? And how d
Except when it is the operand of the sizeof
or unary &
operators, or is a string literal being used to initialize an array in a declaration, an expression of type "N-element array of T
" will have its type implicitly converted to "pointer to T
and will evaluate to the address of the first element in the array.
What does any of that have to do with your question?
Assume the following lines of code:
int arr[10] = {0,1,2,3,4,5,6,7,8,9};
foo(arr);
We pass the array expression arr
as an argument to foo
. Since arr
is not an operand of either sizeof
or &
, its type is implicitly converted from "10-element array of int
" to "pointer to int
". Thus, we are passing a pointer value to foo
, not an array.
It turns out that in a function parameter declaration, T a[]
and T a[N]
are synonyms for T *a
; all three declare a
as a pointer to T
, not an array of T
.
We can write the prototype definition for foo
as
void foo(int *a) // <- foo receives a pointer to int, not an array
or
void foo(int a[]) // <-- a[] is a synonym for *a
Both mean the same thing; both declare a
as a pointer to int
.
Now let's look at multidimensional arrays. Assume the following code:
int arr[10][20];
foo(arr);
The expression arr
has type "10-element array of 20-element array of int
". By the rule described above, it will implicitly be converted to "pointer to 20-element array of int
". Thus, the prototype definition for foo
can be written as
void foo(int (*a)[20]) // <-- foo receives a pointer to an array, not an array of arrays
or
void foo(int a[][20]) // <-- a[][20] is a synonym for (*a)[20]
Again, both declare a
as a pointer, not an array.
This is why you can drop the leftmost (and only the leftmost) array index in a function parameter declaration.