I have tried this but it won\'t work:
#include
int * retArr()
{
int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
return a;
}
For this:
int * retArr() {
int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
return a;
}
The array has local scope and is destroyed as soon as you exit the function. This means that return a;
actually returns a dangling pointer.
To fix this, put the array in global scope, or use malloc()
and copy the array into the allocated space.