A two-dimensional array does not decay to a pointer to pointer to ints. It decays to a pointer to arrays of ints - that is, only the first dimension decays to a pointer. The pointer does not point to int pointers, which when incremented advance by the size of a pointer, but to arrays of 5 integers.
class Myclass {
private:
int myarray[5][5];
public:
typedef int (*pointer_to_arrays)[5]; //typedefs can make things more readable with such awkward types
pointer_to_arrays get_array() {return myarray;}
};
int main()
{
Myclass o;
int (*a)[5] = o.get_array();
//or
Myclass::pointer_to_arrays b = o.get_array();
}
A pointer to pointer (int**
) is used when each subarray is allocated separately (that is, you originally have an array of pointers)
int* p[5];
for (int i = 0; i != 5; ++i) {
p[i] = new int[5];
}
Here we have an array of five pointers, each pointing to the first item in a separate memory block, altogether 6 distinct memory blocks.
In a two-dimensional array you get a single contiguous block of memory:
int arr[5][5]; //a single block of 5 * 5 * sizeof(int) bytes
You should see that the memory layout of these things are completely different, and therefore these things cannot be returned and passed the same way.