Arrays (both one and multidimensional) in c reside in continuous memory blocks. This means that when you define char a[3]
, the array is laid out in memory like this (forgive my terrible ascii art skills):
| a[0] | a[1] | a[2] |
For a two-dimensional array char a[2][3]
, the layout is like this:
| a[0][0] | a[0][1] | a[0][2] | a[1][0] | a[1][1] | a[1][2] |
^
+--- first row ends here
Therefore, when you index into a two-dimensional array a[i][j]
, the compiler generates code equivalent to this:
*(a + i*3 + j)
Which can be read as "skip i rows and take cell j in that row". To accomplish this, the compiler must know the length of the row (which is the second dimension). This means that the second dimension is a part of the type definition!
As such when you want pass a 2d array into a function, you must specify the needed dimension for the type definition.