Passing 2 dimensional arrays to functions

What is the syntax for having a function accept a 2D array as a parameter without the function knowing how large the array that is passed to it will be? Or if that's not possible, how in the world do you just pass a 2D array to a function? None of my books really deal with 2D arrays that much. Thanks in advance

Of course it depends on how you declared the array in the first place - like whether it's a true 2D array or whether its some combination of pointers looking a bit like an array.

> without the function knowing how large the array that is passed to it will be?
You can do this so long as the minor dimension does not change
char foo[10][20];
char bar[100][20];
can be passed to the same function without any problems.

>void foo(int **two_d);
Bzzzt, wrong! That's not a two dimensional array. The following will not compile:

Code:

void foo(int **a);
int main()
{
int array[5][5];
foo(array);
}

The reason is because the whole "array name becomes a pointer" deal only works once per use, so the correct code would be:

Code:

void foo(int (*a)[5]);
int main()
{
int array[5][5];
foo(array);
}

Multidimensional arrays in C are tricky, so I usually recommend duplicating the original declaration in the parameter list:

Code:

void foo(int a[5][5]);
int main()
{
int array[5][5];
foo(array);
}

If you need a multidimensional array without the function knowing the size, you need to simulate a multidimensional array with pointers and memory allocation. Then your example will work, but only if the function's code has a way to get the size of all dimensions somehow.

>but it is in mine (as the parameter name clearly indicates).
You can call it whatever you want, but the type clearly indicates that the argument cannot be declared as a two dimensional array for the reasons that I described. If you want to think of dynamic memory made to look like an array as an actual array that's fine, it's very convenient and I do it myself. But when there are very real syntactic differences, you're expected to make the distinction.