You are not logged in. Please login at to post your questions!


How to return an array from a function?

How to return an array from a function in c++? I know there is a way for it by using pointers but can someone explain how? Also is there an easier way of doing it?

asked 16 Dec '16, 16:38

mathecodician's gravatar image

accept rate: 7%


@mathecodician from where are you learning the Data Structures & Algorithms? and how is your preparation for INOI going?

(17 Dec '16, 19:59) coder_voder2★

@coder_voder My preparation is going good for INOI. I just completed all the past year ZCO problems. I am learning mainly by random online resources. But mostly from websited like khan academy, commonlounge, geekforgeeks, topcoder tutorials, iarcs study material, and mostly from youtube. But mainly I learning by doing practice problems.

(18 Dec '16, 00:24) mathecodician6★

The below way works in both C and C++.

Just declare the array inside a struct. You can return the struct variable from any function.


answered 17 Dec '16, 02:22

likecs's gravatar image

accept rate: 9%


Can you provide the code for how to do it

(17 Dec '16, 18:11) mathecodician6★ go through this, an example, @mathecodician

(17 Dec '16, 20:16) neilit19923★

C++ does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.

If you want to return a single-dimension array from a function, you would have to declare a function returning a pointer as in the following example:

int * myFunction() { . . . } Second point to remember is that C++ does not advocate to return the address of a local variable to outside of the function so you would have to define the local variable as static variable.


answered 16 Dec '16, 18:03

wolfshadow's gravatar image

accept rate: 0%

In C++ we can easily return a VECTOR from a Function .

// Function

vector < int > solve ()


int n, i, value ;

cin>> n;

vector < int > temp;


for( i=0; i < n; i++)


cin >> value;



return temp;


// Calling part :

vector < int> ans;

ans.clear() ;

ans = solve();

for( int i=0; i< ans.size(); i++)

cout << ans[i] << " " ;

//^ you can do whatever Operation you like !

//Hope this Helped. :D


answered 17 Dec '16, 00:51

vikasj554's gravatar image

accept rate: 30%

edited 17 Dec '16, 00:54

I think it doesn't allow return to array. Maybe my knowledge on that still dull. May someone enlighten me please, will greatly appreciate it


answered 19 Dec '16, 06:16

banghasan's gravatar image

accept rate: 8%

Return array from functions in C++ C++ does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.

This answer is marked "community wiki".

answered 28 Dec '16, 16:25

akshat_654's gravatar image

accept rate: 0%

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 16 Dec '16, 16:38

question was seen: 27,262 times

last updated: 28 Dec '16, 16:25