. Advertisement .
..3..
. Advertisement .
..4..
Officially, C++ doesn’t allow arrays as return values, even though many situations require this feature. Check out the methods below to find a workaround and return arrays from functions in C++.
How To Return Arrays From Functions In C++
Using Pointers
In C/C++, pointers can store memory addresses of variables. In addition to manipulating dynamic data structures, these representations allow programmers to pass arguments by reference.
You will need to declare a function that returns a pointer like this:
int* func() {}
This method can be used to return the address of an array, which can be resolved later to access its elements. Keep in mind that returning a local variable’s address is generally not recommended. You should define it as static.
Example:
#include<iostream>
using namespace std;
int* example1()
{
static int x[8]; // Declare an array as static
for(int j = 0; j<=7; j++) {
x[j] = j; // Initialize the array
}
return x; // Return the address of "x"
}
int main()
{
int* ptr;
int j;
ptr = example1(); // Assign the address of "x"
cout<<"The returned array is: ";
for(j=0 ; j<=7; j++)
cout<<"\t"<<ptr[j];
return 0;
}
Output:
The returned array is: 0 1 2 3 4 5 6 7
In the example above, we declare the function example() with type int pointers as its return type. The main() function receives the reference to the array and displays the value of its elements one by one.
Using Structures
Structures in C++ (structs for short) are a data structure that groups several elements together in an object. These elements (called members) can be different data types.
You can put an array inside a struct and return the struct instead.
Example:
#include <iostream>
using namespace std;
// Declare a struct
struct example2
{
// Array as a member of the struct
int array_sample[100];
};
// Declare a function that return a struct
struct example2 return_Function(int m) {
// Declare an object of the struct
struct example2 struc_object;
for(int i=0;i<m;i++) {
struc_object.array_sample[i] = i;
}
// Return the struct object (which contains the array)
return struc_object; //
}
int main()
{
struct example2 a;
int m=7;
a=return_Function(m);
cout<<"The returned array is: ";
for(int i=0;i<m;i++)
{
cout<<"\t"<<a.array_sample[i];
}
return 0;
}
Output:
The returned array is: 0 1 2 3 4 5 6
We declare a struct that has only one array member. The returnFunc() is declared to return a struct. It is then called by main(), which accesses the array through that struct.
Using std::array
std::array is included in the C++ standard library to make array management less of a hassle. It has several advantages over regular arrays. For starters, you can declare a function that returns a std::array.
Example:
#include <iostream>
#include<array>
using namespace std;
// Function with return type std::array
std::array<int, 7> function_sample() {
// Declare a fix-length array with std::array
std::array<int, 7> example_array;
for(int j = 0; j < 7; j++)
{
example_array[j] = j;
}
return example_array;
}
int main() {
std::array<int, 7> return_array;
return_array = function_sample(); // Function call
cout<<"The returned array: ";
for(int j = 0; j < 7; j++)
{
cout<<"\t"<<return_array[j];
}
return 0;
}
Output:
The returned array: 0 1 2 3 4 5 6
The myFunction() returns a std::array directly to main(), which can access its elements.
Conclusion
There are no direct ways to return arrays from functions in C++. But thanks to the existence of many features like pointers, structures, and std::array, you can work around this in different ways.
Leave a comment