讓c++ 函數返回一個數組


在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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM