question abt function returning an array

Hi, today i started to learn abt functions and already i have the 1st difficulties to face with.. my program must give an array to function and after function has done its work, it returns me another array.

Ill give You my code, maybe u can tell me what i am doing wrong here :S

#include <iostream>
#include <iomanip>
#define ARRAY_SIZE 3

using namespace std;

{
int array[ARRAY_SIZE];
int i=0;
int array_returning( int mas[] ); //define function prototype
cout<<"Please enter an array!"<<endl;
for(i=0; i<3; i++)
{
cout<<"IEnter arrays "<<i+1<<". element: ";
cin>>array[i];
};

cout<<"Function returns this array: "<<endl;
cout<<array_returning(array);
system("pause");

return 0;
}

int array_returning(int mas[])
{
int *myptr;
myptr=mas;

return *myptr;
}

I have browsed web and understod that u cannot return array simply like that, but u can return a pointer which references to this array. So i created pointer *myptr and made it to point to the array mas[]. But it just doesnt want to print out me the array which function has returned :(
oviously i am doing something wrong here but i just cannot find what it is :S

this was just an example, originally i must pass to the function 3 different arrays (all same size ) and then make from these 3 arrays one big array and return this big array to main function..

i hope You guys can help me ;)
[1718 byte] By [errtime] at [2007-11-20 10:56:14]
# 1 Re: question abt function returning an array
When posting code, place the code within code forum bbcode tags.

maybe u can tell me what i am doing wrong here
For starters, there is no main function.

i must pass to the function 3 different arrays (all same size ) and then make from these 3 arrays one big array and return this big array to main function
Generally, there are two ways to pass an array:
a) Pass the array along with its size. The size is needed since the array decays to a pointer when passed, hence sizeof() would not return the size of the array
b) Pass pointers to the start and (one past) the end of the array

If you know the array size at compile time, then you could just create the big array, pass it to the function along with the other 3 arrays, and then have the function combine the smaller arrays into the big array. There would be no need to return anything.
laserlight at 2007-11-9 1:24:47 >
# 2 Re: question abt function returning an array
Generally, there are two ways to pass an array:
a) Pass the array along with its size. The size is needed since the array
decays to a pointer when passed, hence sizeof() would not return the size of the array
b) Pass pointers to the start and (one past) the end of the array

a) how?
b)like this?

int array_returning(int mas[])
{
int array[5];
int *startptr;
startptr=array; //point to 1st element of an array
int *endptr;
endptr=?? //how to tell pointer to point to the '0/' element of an array? array last element+1? in our current variant endptr=array[6]?

return *startptr, *endptr;
}
errtime at 2007-11-9 1:25:49 >
# 3 Re: question abt function returning an array
1) You can do something like this:

void function( int* array, int count)
{
cout<<"Please enter an array!"<<endl;
for(int i = 0; i < count; ++i)
{
cout << "IEnter arrays " << i + 1 << ". element: ";
cin >> array[i];
}
}

You don't need to return anything here, since the array is passed as a pointer, and anything that happened to the array inside function will have effect outside of it.

2) If you want to return an array, you have to return a pointer. It would look something like this

int* function ( int count )
{
int* array = new int[count];
cout<<"Please enter an array!"<<endl;
for(int i = 0; i < count; ++i)
{
cout << "IEnter arrays " << i + 1 << ". element: ";
cin >> array[i];
}
return array;
}

3) You can also use vector template from STL (standard template library), than you can use it like a normal variable, since it's a class.
It would look something like this:

vector<int> function ( int count )
{
vector<int> array;
for ( int i = 0; i < count; ++i )
{
int tmp;
cout << "IEnter arrays " << i + 1 << ". element: ";
cin >> tmp;
array.push_back(tmp);
}
return array;
}

Additionally you don't need to store the vector's size, since it's already stored in it's member variable for you. If you want to print out the vector's content you can do something like this:

vector<int>::iterator it;
for ( it = array.begin(); it < array.end(); ++it )
cout << *it << endl;
sebus at 2007-11-9 1:26:45 >