×

# How to return an array from a function?

 0 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 2.6k●1●10●34 accept rate: 7% 2 @mathecodician from where are you learning the Data Structures & Algorithms? and how is your preparation for INOI going? (17 Dec '16, 19:59) 2 @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)

 6 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 6★likecs 3.7k●24●81 accept rate: 9% 2 Can you provide the code for how to do it (17 Dec '16, 18:11) http://ideone.com/ObRMZD go through this, an example, @mathecodician (17 Dec '16, 20:16)
 0 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 1 accept rate: 0%
 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; temp.clear(); for( i=0; i < n; i++) { cin >> value; temp.push_back(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 219●8 accept rate: 30%
 0 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 32 accept rate: 8%
 0 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. link This answer is marked "community wiki". answered 28 Dec '16, 16:25 1 accept rate: 0%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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:

×1,916
×862
×62
×46

question asked: 16 Dec '16, 16:38

question was seen: 27,262 times

last updated: 28 Dec '16, 16:25