Return an Array from Function in C++
In C++, you can write functions that return arrays. Since arrays are not directly returnable by value, alternative methods like pointers, std::array
, or std::vector
are often used.
In this tutorial, we will cover different approaches to return arrays from functions.
Approaches to Return an Array from a Function
- Returning a pointer to an array
- Using
std::array
(C++11 and later) - Using
std::vector
Approach 1 – Returning a Pointer to an Array
You can return a pointer to an array from the function. Let us see the syntax and an example.
Syntax
</>
Copy
return_type* function_name() {
// Define and return a pointer to an array
}
- return_type*
-
Indicates that the function returns a pointer to an array of the specified type. For example:
int*
: A pointer to an integer or an array of integers.float*
: A pointer to a floating-point value or an array of floats.
- function_name()
-
The name of the function followed by parentheses. The parentheses may include parameters if the function requires input. For example:
int* getArray()
: A function that takes no arguments and returns a pointer to an integer array.int* getArray(int size)
: A function that takes an integer parameter (size
) and returns a pointer to an integer array.
- { … }
-
Encloses the body of the function, where the logic to create or reference an array is implemented. The function typically:
- Defines or initializes an array.
- Returns a pointer to the first element of the array.
Example
</>
Copy
#include <iostream>
using namespace std;
int* generateArray() {
static int arr[5] = {1, 2, 3, 4, 5};
return arr;
}
int main() {
int* result = generateArray();
for (int i = 0; i < 5; i++) {
cout << result[i] << " ";
}
return 0;
}
Explanation
- The function
generateArray
returns a pointer to a static arrayarr
. - In the
main
function, the pointer is used to access and print the array elements. - Note: Returning a pointer to a local array would result in undefined behavior, so a static array is used.
Output
1 2 3 4 5
Approach 2 – Return an Array from Function using std::array
std::array
is a fixed-size container introduced in C++11 that can be returned directly from a function.
Example
</>
Copy
#include <iostream>
#include <array>
using namespace std;
array<int, 5> generateArray() {
return {1, 2, 3, 4, 5};
}
int main() {
array<int, 5> result = generateArray();
for (int value : result) {
cout << value << " ";
}
return 0;
}
Explanation
- The
generateArray
function returns anstd::array
of size 5. - The
std::array
can be returned and copied safely without using pointers.
Approach 3 – Return an Array from Function using std::vector
std::vector
is a dynamic array that can grow or shrink in size and is ideal for returning arrays when the size is not fixed.
Example
</>
Copy
#include <iostream>
#include <vector>
using namespace std;
vector<int> generateArray() {
return {1, 2, 3, 4, 5};
}
int main() {
vector<int> result = generateArray();
for (int value : result) {
cout << value << " ";
}
return 0;
}
Explanation
- The
generateArray
function returns anstd::vector
containing the array elements. - The
std::vector
can be returned safely, and it manages its own memory.
Comparison of Methods
Method | Advantages | Disadvantages |
---|---|---|
Pointer to Array | Low overhead, works in C++98. | Requires careful memory management; static arrays have limited scope. |
std::array | Type-safe, no need for pointers, fixed size. | Size must be known at compile-time. |
std::vector | Dynamic size, easy to use, manages its own memory. | May have higher overhead compared to static arrays. |