How can one create a function that will accept arbitrary length multidimensional arrays?
eg,
int foo[3][4];
int foo2[5][10];
int foo3[x][y];
int somefunction(???)
{
/*accept input in the form of any array and process accordingly.*/
}
How can one create a function that will accept arbitrary length multidimensional arrays?
eg,
int foo[3][4];
int foo2[5][10];
int foo3[x][y];
int somefunction(???)
{
/*accept input in the form of any array and process accordingly.*/
}
int somefunction(int foo[][4]);
or you can try
int somefunction(int **foo);
just to thoroughly test things out
The compiler must know the size of the second and any subsequent dimensions when using array notation, and pointer notation can get confusing fast.
I've already tried that. When you try
int somefunction(**foo) and call
somefunction(foo)
you get an error "can't convert from int [3][4] to int **"
Conversely, I'm sure the int somefunction(*foo[4]) would work perfectly for arrays with the second dimension being 4, but I'm trying to create a function with arbitrary input. Eg, you could call the same function with a 4x4 array, a 12x30 array, a 2x5 array, etc.
Thanks for the help though,
-Max
What I was trying to get at before was I don't believe there is a way to pass a multi-dimensional array of any size to a function, the function has to know the lengths of the latter dimensions.
Though you can have a multi-dimensional array that is the maximum size and pass that to the function, then have another array the same size but only fill what you need and have the rest as NULL. Then you can pass an array of any size to a function and have a good way to process it. This gets to be a problem when the arrays get very large though
ex.
int foo[20][10] = {0}; //gets filled completely
int bar[20][10] = {0}; //gets filled half-way
int baz[20][10] = {0}; //only has a few elements