about return an array

OK. I get it that to return multiple variables from a function is a dead end. And to use global variables is simply too messy when the program grows bigger. And if I use pointers, well, god knows whether the memory space the functions created still exists when the "return" is performed. And so, I really have no idea how to pass an array back with a "return". Any one has a better idea?
Thanks very much for the help...
You could pass the array and array size in the parameters of the function call and get your changes back that way.

A real quick example:

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
38
39
40
41
42
#include <iostream>
#include <ctime>

using namespace std;

#define ARRAYSIZE 10

int change_array( int *, int );

int main( int arc, char *argv[] )
{
	int array[ARRAYSIZE];
	int recvresult;


	cout << "Array before function call" << endl;

	for( int i = 0; i < ARRAYSIZE; i++ )
		cout << array[i] << endl;

	recvresult = change_array( array, ARRAYSIZE );

	cout << endl;
	cout << "Array after funtion call" << endl;

	for( int i = 0; i < ARRAYSIZE; i++ )
		cout << array[i] << endl;

	cin.get();
}

int change_array( int *ar1, int ars )
{
	srand( time(NULL ));

	for( int i = 0; i < ars; i++ )
	{
		ar1[i] = rand();
	}

	return true;
}

I get it that to return multiple variables from a function is a dead end

map::insert, std::mismatch, and many other standard functions return two values each. But yes, with multiple values, it's better to return a container

I really have no idea how to pass an array back with a "return".

Why array? Return a vector

1
2
3
4
5
6
vector<int> some_function()
{
   vector<int> v;
   // do stuff that fills it up
    return v;
}
create your own array class
or go to http://sourceforge.net/projects/ and search for some Matrix C++ Library have a look at their documentation first, than see which one is better has documented and is easy for you. Download it and learn from their codes and programming techniques.
Last edited on
Don't create you own class, use a standard container (like vector).
You learn only if you create yours, you may have bugs, errors in your code, but you lear by doing, but not by using STL :-)
Last edited on
@therockon7throw would you recommend writing your own std::cout before learning how to use the one provided by the standard library? First learn how to use basic C++, then how to extend it.
You can return an STL container from a function but if it is big then there will be a lot of copying going on.

Another option is to pass a non-const reference to the container into the function and then fill the the container in the function.

A third option is to use a reference counted object to avoid the deep copying going on. Either make this yourself or use a smart pointer such as boot::shared_ptr
mik2718 wrote:
You can return an STL container from a function but if it is big then there will be a lot of copying going on.
Most compilers today will do RVO to avoid the copying. In C++11 if you return a local vector, it is guaranteed that there will be no copying of the elements.
Last edited on
I find using a structure useful for my case, it permit me passing multiple values of different types. Thanks, guys~
1
2
3
4
int* foo (int* array)
{
      return array;
      }


most simple array-returning function. I dont get why is that so difficoult!
Last edited on
@viliml, How was a allocated? Do the caller need to delete the array?
1
2
3
4
5
6
7
8
9
10
11
12
int*foo (int* array)
{
      return array;
      }

int main()
{
      int a[3]={1, 2, 3};
      int* b=foo (a);
      std::cout<<b[0]<<" "<<b[1]<<" "<<b[2];
      return 0;
      }
1 2 3

Sorry, return a; was a mistake, I meant return array;
Topic archived. No new replies allowed.