In this article, we will see how to return array from function in C++.
It is not possible to directly return an array to a function call and it can be done by using pointers. If you declare a function with a pointer return type that returns the address of the C-type array, then it will not work all the time in C++. You will get a warning message from the compiler and it can also show some abnormal behaviour in the output.
In this article, we have mentioned five methods to return an array from a function in C++.
Table of Contents
Using a static array with pointers
We can handle the abnormal behaviour of the normal array by declaring it as static. It is the only way to avoid warnings and unwanted results.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> using namespace std; int *joy() //declaring function as a pointer type { static int array[100]; //declaring static array //performing operations on the array array[0] = 101; array[1] = 102; array[2] = 103; array[3] = 104; array[4] = 105; return array; } int main() { int* pointer = joy(); //function calling //printing the elements stored in the array cout << pointer[0] << " " << pointer[1]<< " " << pointer[2] << " " << pointer[3]; return 0; } |
Output:
Using dynamically allocated array
Arrays can be dynamically allocated using malloc()
or new
and they will remain there until we delete them. We will delete the array after coming out of the function.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> using namespace std; int *joy() //declaring function as a pointer type { int *array= new int[100]; //dynamically allocating memory //performing operations on the array array[0] = 101; array[1] = 102; array[2] = 103; array[3] = 104; array[4] = 105; return array; } int main() { int* pointer = joy(); //function calling //printing the elements stored in the array cout << pointer[0] << " " << pointer[1]<< " " << pointer[2] << " " << pointer[3]; delete[] pointer; return 0; } |
Output:
Using structs
We can declare an array inside a structure in C++
. We can return an instance of the struct because the elements in the array of structures are copied deeply.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include <iostream> using namespace std; struct java2blog { //Declaring an array inside struct with size 100. int arr[100]; }; struct java2blog fun(int n) //Here the return type is struct java2blog { struct java2blog a; //java2blog structure member declared for(int i=0;i<n;i++) { //array initialisation a.arr[i] = i; } return a; //address of structure member returned } int main() { struct java2blog a; int n=10; //size of the array a=fun(n); //address of the array cout<<"Elements present in the array are : "; for(int i=0;i<n;i++) { cout<<a.arr[i]<<" "; } return 0; } |
Output:
Using std:: array
C++ also offers another choice for returning an array from a function – std:: array
. It is a structure template that wraps a regular array and it also provides extra methods like size() & empty(). We can return the array name to return the whole array at the time of the function call. We also need to include the header file ‘array’ to use this method.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <iostream> #include<array> using namespace std; array<int,10> function() //function declared with return type array<> { array<int,10> arr; //array declared int c=0; for(int i=0;i<10;i++) { //filling elements in the array arr[i] = c; c++; } return arr; //array returned } int main() { array<int,10> arr; //array with length 10 arr=function(); //function calling cout<<"Elements in the array are: "; for(int i=0;i<10;i++) { cout<<arr[i]<<" "; } return 0; } |
Output:
Using vector container
We can store our array elements in a vector container and its size can increase or decrease dynamically. There is no need for a size parameter in this case.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include <vector> using namespace std; vector<int> addTenToArray(const vector<int> *arr) { vector<int> vect{}; for(const auto &items : *arr){ vect.push_back(items+10); } return vect; } int main() { vector<int> array_vect={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; auto result_array = addTenToArray(&array_vect); cout << "Array = [ "; for (int i : result_array) { cout << i << ", "; } cout << "]" << endl; return 0; } |
Output:
Conclusion
In this article, we discussed five different methods to return array from function in C++. Any method can be used as per the requirements.
Happy Learning!!