Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wrote a function which returns an array; so how can I save (copy) that returned array into another array. What is the best and fastest way?

C++
//function declaration
int func (int array[]);

// function defenition
int func (int array[]){
     ....
    return result[];
}
Posted
Comments
Philippe Mori 5-Aug-15 13:30pm    
Write real C++ code using STL containers like std::vector. It does not make much sense to use C style array in 2015.
amin.j 5-Aug-15 13:55pm    
Dear @Philippe, what are your suggestions for me to write real C++ code? I mean what is the fastest and straightforward way to learn that? Is there any tutorial or valuable book or whatever else?
Philippe Mori 5-Aug-15 14:28pm    
Use Google with words like: C++ tutorial std vector. One link would be http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm

There are also hundrends of book on C++.

Rather than trying to learn C++ by posting questions here, your time would really be better spent getting hold of a good book on the subject and working through it. Looking at this and the other question you posted, there seems to be a lack of understanding of some of the basics.

 
Share this answer
 
Comments
[no name] 5-Aug-15 22:10pm    
It had to be said.

You function should be declared this way


C++
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:


C++
 #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};
 	// length of a and b
	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)
C++
#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]
 
Share this answer
 
v7
Comments
amin.j 5-Aug-15 5:34am    
You mean I use vector instead of pointer?
CPallini 5-Aug-15 6:09am    
Use a vector<int> instead of an array of int.
amin.j 5-Aug-15 8:10am    
Dear @CPallini, I did what you said, program is running now after debugging but I got an strange output something like that: 4765800241! Do you think I'm doing wrong?

int addResult, updLen;
int diffLen = abs(lenStr1 - lenStr2);
if (lenStr1 > lenStr2){
updLen = lenStr1;
int updDisStr2[updLen];
lenEqualize(updDisStr2, equiDisStr2, diffLen, updLen);
cout << updDisStr2[0]; // here is the point
}
else if (lenStr1 < lenStr2){
updLen = lenStr2;
int updDisStr1[updLen];
lenEqualize(updDisStr1, equiDisStr1, diffLen, updLen);
cout << updDisStr1[0];
}
else if (lenStr1 == lenStr2){
updLen = lenStr1;
}



void lenEqualize(int* updDisStr, int* shortLenStr, int diffLen, int updLen){
for (int i = 0; i < updLen; i++){
if (i < diffLen-1){
updDisStr[i] = -1;
}
else {
updDisStr[i] = shortLenStr[i-diffLen];
}
}
}
CPallini 5-Aug-15 8:29am    
What is supposed to do, your code (I really didn't get it)?
amin.j 5-Aug-15 8:39am    
I have two arrays: equiDisStr1 and equiDisStr2
I want to compare these two arrays and if the size of them are not equal, make it equal by defining new array with the maximum size of two arrays; in fact, I fill the remainder of new array by -1 (which is a flag for me).

--- for example

equiDisStr1 : 2345
equiDisStr2: 3457604

So: size(equiDisStr1) < size(equiDisStr2) ---->
Output: updDisStr == -1-1-12345
equiDisStr2 == 3457604
now, size of both are 7. And based on the other parts of the program, I need to do this way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900