You are not logged in. Please login at www.codechef.com 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

6★mathecodician
2.6k11034
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) coder_voder2★
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) 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.

link

answered 17 Dec '16, 02:22

likecs's gravatar image

6★likecs
3.7k2481
accept rate: 9%

2

Can you provide the code for how to do it

(17 Dec '16, 18:11) mathecodician6★

http://ideone.com/ObRMZD 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.

link

answered 16 Dec '16, 18:03

wolfshadow's gravatar image

1★wolfshadow
1
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;

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

link

answered 17 Dec '16, 00:51

vikasj554's gravatar image

4★vikasj554
2198
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

link

answered 19 Dec '16, 06:16

banghasan's gravatar image

0★banghasan
32
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.

link
This answer is marked "community wiki".

answered 28 Dec '16, 16:25

akshat_654's gravatar image

1★akshat_654
1
accept rate: 0%

toggle preview
Preview

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "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:

×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