Search code examples
carraysarray-address

Passing multi-dimensional arrays to functions in C


Why is it necessary to specify the number of elements of a C-array when it is passed as a parameter to a function (10 in the following example)?

void myFun(int arr[][10]) {}

Is it so because the number of elements is needed to determine the address of the cell being accessed?


Solution

  • Yes. It's because arr[i][j] means ((int *)arr)[i * N + j] if arr is an int [][N]: the pointer-arithmetic requires the length of a row.