C++ Returning Arrays from Functions
C++ does not allow returning a complete array as a function argument. However, you can return a pointer to an array by specifying the array name without an index.
If you want to return a one-dimensional array from a function, you must declare a function that returns a pointer, as follows:
The following example:
Example
{
int myArray[3] = {1, 2, 3};
return myArray;
}
Note:You cannot simply return a pointer to a local array, because when the function ends, the local array will be destroyed, and the pointer to it will become invalid.
C++ does not support returning the address of a local variable outside the function, unless the local variable is defined asstaticvariable.
To avoid the above situation, you can use a static array or a dynamically allocated array.
Using a static array requires creating a static array inside the function and returning its address, for example:
{
static int myArray[3] = {1, 2, 3};
return myArray;
}
Now, let's look at the following function, which generates 10 random numbers and uses an array to return them, as follows:
Example
When the above code is compiled and executed, it produces the following results:
624723190 1468735695 807113585 976495677 613357504 1377296355 1530315259 1778906708 1820354158 667126415 *(p + 0) : 624723190 *(p + 1) : 1468735695 *(p + 2) : 807113585 *(p + 3) : 976495677 *(p + 4) : 613357504 *(p + 5) : 1377296355 *(p + 6) : 1530315259 *(p + 7) : 1778906708 *(p + 8) : 1820354158 *(p + 9) : 667126415
Using a dynamically allocated array requires using the new operator inside the function to allocate an array, and using the delete operator to release the array when the function ends, for example:
Example
{
int* myArray = new int[3];
myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;
return myArray;
}
int main()
{
int* result = myFunction();
// Use result
delete[] result;
return 0;
}
The following is a simple application example that demonstrates how to return an array from a function:
Example
using namespace std;
int* createArray(int size) {
int* arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = i + 1;
}
return arr;
}
int main() {
int* myArray = createArray(5);
for (int i = 0; i < 5; i++) {
cout << myArray[i] << " ";
}
cout << endl;
delete[] myArray; // release memory
return 0;
}
In the above example, we declared a function named createArray, which accepts an integer parameter size and returns an integer array filled with integers. We used the new operator to dynamically allocate an array on the heap and filled the array inside the function. Finally, the function returned a pointer to the array.
In the main function, we called the createArray function and stored the returned array pointer in myArray. Then we iterated over the array and printed the value of each element. Finally, we used the delete[] operator to release the memory occupied by myArray to avoid memory leakage.
When the above code is compiled and executed, it produces the following results:
1 2 3 4 5
Note:When using a dynamically allocated array, the code that calls the function is responsible for releasing the returned array. This is because the array allocated inside the function is not automatically released when the function ends.
other extensions