开发者

returning array from a function [duplicate]

开发者 https://www.devze.com 2023-03-03 22:28 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: howto return a array in a c++ method?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

howto return a array in a c++ method?

开发者_JAVA百科

How an array can be returned from a function in c++?please accomplish your answer with a simple example too if possible.thankx in advance.


Return a pointer to the start of the array, like:

int* getArray(int numElements) {
   int* theArray = malloc(sizeof(int) * numElements);
   return theArray;
}

...you can use it like:

int* myArray = getArray(3);
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;

//do this when you are done with it
free(myArray);
0

精彩评论

暂无评论...
验证码 换一张
取 消