how define a function that return an array

hello .
how can i define a that it return an array;
1
2
3
4
5
6
type fname(){
type array[10]={0};
...
...
...
return array;

}
please guide me with a good code
Last edited on
You can't return local C arrays from a function.

However, you can do this if your array size is fixed:
1
2
3
4
5
6
#include <array>
array<int,10> getArray()
{
  array<int,10> array={0};
  return array;
}


Or this if it isn't:
1
2
3
4
5
6
#include <vector>
vector<int> getArray2()
{
  vector<int> vec(10);
  return vec;
}


The first example requires your compiler to support C++0x or TR1, so the second one might be preferable either way.
The problem with an array is that you must also know the size of the array. Hence, an STL container like the std::vector is nice, since it does all the bookkeepping for you.

If you must, however, you can also just use one of the following C methods:

    Fill an existing array
1
2
3
4
5
6
void myfn( int* a, unsigned alen )
  {
  // Fill the array a[ alen ] here.
  // In this case, the array a already exists with alen elements.
  // This function only fills it.
  }
1
2
int xs[ 10 ];
myfn( xs, 10 );

    Use references (C style)
1
2
3
4
5
6
void myfn( int** a, unsigned* alen )
  {
  *alen = size_of_the_array_to_create;
  *a = new int[ *alen ];
  ...
  }
1
2
3
4
5
int* xs;
unsigned xlen;
myfn( &xs, &xlen );
...
delete [] xs;

    Use references (C++ style)
1
2
3
4
5
6
void myfn( int*& a, unsigned& alen )
  {
  alen = size_of_the_array_to_create;
  a = new int[ alen ];
  ...
  }
1
2
3
4
5
int* xs;
unsigned xlen;
myfn( xs, xlen );
...
delete [] xs;

    Return array of a specific size
1
2
3
4
5
6
int* myfn( unsigned len )
  {
  int* a = new int[ len ];
  ...
  return a;
  }
1
2
3
4
int* xs;
xs = myfn( 10 );
...
delete [] xs;

    Variations on a theme
1
2
3
4
5
6
unsigned myfn( int*& a )
  {
  a = new int[ 10 ];
  ...
  return 10;
  }
1
2
3
4
5
int* xs;
unsigned xlen;
xlen = myfn( xs );
...
delete [] xs;

    Use a struct (C method)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct myarray
  {
  int* a;
  unsigned length;
  };
myarray myfn()
  {
  myarray a;
  a.a = new int[ 10 ];
  a.length = 10;
  a.a[ 0 ] = 41;
  ...
  return a;
  }
1
2
3
4
myarray xs = myfn();
for (unsigned n = 0; n < xs.length; n++)
  cout << xs.a[ n ] << endl;
delete [] xs.a;

    Use a struct or class (C++ method)
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
struct myarray
  {
  int* a;
  unsigned length;
  // Useful C++ stuff follows
  myarray( unsigned len ):
    a( len ? (new int[ len ]) : NULL ),
    length( len )
    { }
  ~myarray()
    {
    if (a) delete [] a;
    }
  int& operator [] ( unsigned n )
    {
    return a[ n ];
    }
  const int& operator [] ( unsigned n ) const
    {
    return a[ n ];
    }
  };
myarray myfn()
  {
  myarray a( 10 );
  a[ 0 ] = 42;
  ...
  return a;
  }
1
2
3
4
myarray xs = myfn();
for (unsigned n = 0; n < xs.length; n++)
  cout << xs[ n ] << endl;
// no need to delete anything here... the destructor does it for you... 

Hope this helps.
Last edited on
Topic archived. No new replies allowed.