You function should be declared this way
int * func(int array[]);
Please note: you should either return a static
array or a dynamically allocated (e.g. using new
) one. The latter technique requires the caller to delete the array in order to prevent memory leaks.
sample code:
#include <iostream>
int * func(int array[])
{
static int b[]={1,2,3};
return b;
}
int main()
{
int a[] = {4,5,6};
int * p = func(a);
std::cout << p[2] << std::endl;
}
It is worth nothing such code is prone to errors, it's probably better using
std::vector
instead.
[update]
Here you are a
C
-like version
#include <iostream>
using namespace std;
void dump(int * x, int lx);
void pad(int * d, int ld, int *s, int ls);
int main()
{
int a[] = {1,2,3};
int b[] = {4,6,8,10,12};
int la = sizeof(a)/sizeof(a[0]);
int lb = sizeof(b)/sizeof(b[0]);
int lp = 0;
int * p = NULL;
if ( la > lb)
{
lp = la;
p = new int[lp];
pad(p, lp, b, lb);
}
else if (la < lb)
{
lp = lb;
p = new int[lp];
pad(p, lp, a, la);
}
dump(a, la);
dump(b, lb);
dump(p, lp);
if ( p ) delete [] p;
}
void dump(int * x, int lx)
{
for (int n=0; n<lx; ++n) cout << x[n] << " ";
cout << endl;
}
void pad(int * d, int ld, int *s, int ls)
{
int n;
for (n=0; n<(ld-ls); ++n)
d[n] = -1;
for ( ;n<ld;++n)
d[n] = s[n-ld+ls];
}
[/update]
[update 2]
More similar to
C++
(you need a modern compiler)
#include <iostream>
#include <vector>
using namespace std;
void dump( vector<int> v );
int main()
{
vector <int> a{1,2,3};
vector <int> b{4,6,8,10,12};
vector <int> p;
if ( a.size() > b.size())
{
p.insert(p.end(), a.size()-b.size(),-1);
p.insert(p.end(), b.begin(), b.end());
}
else if (a.size() < b.size())
{
p.insert(p.end(), b.size()-a.size(), -1);
p.insert(p.end(),a.begin(), a.end());
}
dump(a);
dump(b);
dump(p);
}
void dump( vector<int> v )
{
for (auto x : v)
cout << x << " ";
cout << endl;
}
[/update2]