I've just started learning C++ and am wondering if there is a way of checking whether a passed array is 2d? (The reason I've mentioned Error Checking in the title is because I'm wondering if this can be done by trying to reference, say, array[0][0] and seeing if an error arises, but then I don't know how error-trapping works in C++)
There's only one way, and it may not necessarily work: T f(T array[width][height]);
In order to do array[0][0], one of these two conditions have to be met:
* 'array' is of type T[][]. That is, it was declared as T array[][]={/*...*/};, or T array[width][height];
* 'array' is of type T**.
Dereferencing is done at run time, but these conditions will be checked by the compiler at compile time, so there's really no point in doing it.
Even if that wasn't the case, if you were to dereference something you shouldn't dereference, the OS is likely to crash your program.
Bottom line, there is no sure-fire way to know that the pointer that was passed points to something that makes sense. The function should just assume that it does and give the responsibility of passing valid data to the caller.
The only way you would know is if you had function overloading, whereby one function would accept only one array as an argument and another function that accepted two, and recognised the activity within that function.
I think this will do the job jsmith. Thanks. Could you explain to be what a template is though please, or perhaps provide a link to what you think is a decent explanation of it?