When an array is passed directly as parameter to a function (pass by value) it decays into a pointer to the first element of the array. Even if you can clearly read in the signature the dimensions of the array, those dimensions are ignored by the compiler. That behavior is compatible with C.
Using C++ you can pass the array by reference and that would not be a problem any more.
int extract_value( int (&a)[10][10], int row, int col ) {
return a[row][col];
}
int main() {
int a[10][10] = {};
a[5][5] = 1;
std::cout << extract_value( a, 5, 5 ) << std::endl;
int b[5][5];
// extract_value( b, 2, 2 ); // error: the function takes an array of 10x10
}
The function parameter must match exactly, that is, it only takes an array of 10x10 elements. You can take rid of that restriction by templating the function on the array sizes. Once you are at it also the type:
template <typename T, int Rows, int Cols>
T extract_value( T (&a)[Rows][Cols], int row, int col ) {
return a[row][col];
}
int main() {
int a[5][7] = {};
extract_value( a, 3, 4 );
int b[8][2] = {};
extract_value( b, 7, 1 ); // correct, the compiler matches sizes
double c[4][4] = {};
extract_value( c, 2, 2 ); // different types are allowed
}
This solution is still cumbersome in that the sizes must be compile time constants, and the array must be stack allocated. The solution to this is defining some class that takes dynamic memory in a buffer (linear) and has a conversion from the N-coordinate system into the 1-dimensional array to obtain values, as it was suggested before. You can get some hints on how to do it in this FAQ about operator overloading that provides an implementation of a 2D matrix. Once you have that implemented, you can just use that as parameter to functions/methods.
My recommendation would be to follow this last path: encapsulate the N-dimensional array into a class that provides conversions into a 1D vector (the C++FAQ lite uses a raw pointer, I prefer STL containers).