开发者

returning array from a function [duplicate]

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜