You cannot return an array.
You can only return a type or a pointer and an array is not a type. Rather, it is a container of some type.
However, in C++, the name of an array is the address of element 0 which makes the name of an array a pointer. - and you can return a pointer.
So if you allocate an array of int:
int* array = new int[100];
then you can return the name "array". Your function would have an int* return value.
This only works with one-dimensional arrays.
With a 2D array, the name of the array is the address of element 0 which is now also an array. You cannot return the address of an array. Again, that's because it's not the address of a type.
In this case, you use a pointer-to-pointer argument in the function.
-
void func(int (**arg)[3] )
-
{
-
*arg = new int[3][3];
-
}
-
You call the function with:
-
int (*ptr)[3]; //pointer to an array of 3 int
-
-
func(&ptr); //call function with address of pointer.
-
//the function will put the address of the array
-
//in the pointer
-