在c++中是不允許數組作為函數的返回值的
int [] someFunction( ); //ILLEGAL
要想實現函數返回一個數組,那返回對應數組里面類型的指針
you must return a pointer to the array base type and have the pointer point to the array. So, the function declaration would be as follows:
int* someFunction( ); //Legal
int* doubler( int a[], int size)
{
int* temp = new int[size];
for ( int i =0; i < size; i++)
temp[i] = 2*a[i];
return temp;
}
調用
int a[] = {1, 2, 3, 4, 5};
int* b;
b = doubler(a, 5);
delete[] b