First, an array created this way is not technically the same type as int**, so that shouldn't even compile.
Additionally, this will not work because when you create the array m[10][10], you are creating it on the stack, within the scope of my_method(). Once my_method() ends, any local variables will go out of scope (no longer exist). You can create the array on the heap using the
new
operator as follows:
1 2 3 4 5 6 7 8 9 10
|
int ** my_method()
{
int **m = new int*[10];
for (int i=0; i<10; i++)
{
m[i] = new int[10];
}
return m;
}
| |
However, this is generally not a good idea, since you must remember to use
delete
later in your code, or you will have a memory leak. Creating the array outside the method and passing it as an argument is generally a better way of doing things.
Arrays and dynamic memory are usually painful for beginners, so I'd suggest you read:
http://cplusplus.com/doc/tutorial/arrays/
... before venturing further.