In the general case, it may be best to pass the dimensions of the array and the address of its first element to the function (that is, to represent this array as one-dimensional in the function) and independently calculate the required index when accessing the elements of the array.
For example, for a two-dimensional array:
void pri_arr (int ncol, int nlines, int a[]) { int i, j; for(i = 0; i < nlines; i++) for (j = 0; j < ncol || (puts(""), 0); j++) printf("%d ", a[i * ncol + j]); } int main() { puts("2 dim"); int a[3][4] = {{1, 2, 3, 4}, {11, 12, 13, 14}, {21, 22, 23, 24}}; pri_arr(4, 3, &a[0][0]); }
This method works in all C and C ++ compilers.
But in gcc (C, not C ++), the same example can be written in a more natural way:
void pri_arr (int ncol, int nlines, int a[][ncol]) { int i, j; for(i = 0; i < nlines; i++) for (j = 0; j < ncol || (puts(""), 0); j++) printf("%d ", a[i][j]); }
Note that the number of elements in an array string must be passed before it is declared in the list of arguments.
int main() { puts("2 dim"); int a[3][4] = {{1, 2, 3, 4}, {11, 12, 13, 14}, {21, 22, 23, 24}}; pri_arr(4, 3, a); }
When calling, we pass the array itself (and not the address of the first element, as in the first example).