c++ : declaring function that return multi array

Carlis

Senior member
May 19, 2006
237
0
76
I took up c++ today to do some numerical work. Now, I want to use some multi arrays in my functions. But I keep getting errors. My declaratiion goes something like this;

function declaration at top;

double myfunc[][](double[][] ); //line 14 is here...

...

And then the function;

double myfunc[][](double d[][]){
double out[3*N][3*N];

( do stuff to define out)

return out;
}


Apparently this wont do it.



tmain.cpp:14: error: expected unqualified-id before '[' token... and line 14 is the first one here...

How does one properly declare it?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Either return the array as a parameter or a pointer
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
There isn't really a non destructive way to return an array from a function. Especially considering what the underlying data type is, a pointer.

Instead, do something like this

void myfunc(double in[][], out[][])
{
( do stuff to define out)
}

out doesn't have to be passed in by reference or anything, it just has to be created and made the same size as in.

The other option is to use a vector instead. You can safely return a vector from a function. So it would look something like

vector< vector < double > > myFunc(vector < double > d)
{
vector < vector < double > > out;
(do stuff to out)
return out;
}
 

Carlis

Senior member
May 19, 2006
237
0
76
Aha! Now I get it. Im used to these new languages like java and python, which are designed for lazy people like my self. Trouble is, they are so damn slow for some applications. Thank you very much for the assistance.
/Carlis
 

blahblah99

Platinum Member
Oct 10, 2000
2,689
0
0
You can't return an array declared within a function due to the scope of the variable. As soon as the function returns, any variables declared within the function are destroyed.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Try passing by reference instead. e.g.:

#include <vector>in
using namespace std;

void myFunction( const vector<vector<double> > &in, vector<vector<double> > &out ) {

}

/* out can be modified in myFunction, and changes will be visible to callers */

/* Don't forget to properly size in and out, e.g. */
void main() {
vector<vector<double> > in, out;
in.resize(3);
out.resize(3);
for(int k=0;k<3;k++) {
in[k].resize(3, 0.0);
out[k].resize(3, 0.0);
}

myFunction(in,out);
}

EDIT: Changed loop iterator from i to k to prevent formatting.
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
Originally posted by: degibson
Try passing by reference instead. e.g.:

#include <vector>in
using namespace std;

void myFunction( const vector<vector<double> > &in, vector<vector<double> > &out ) {

}

/* out can be modified in myFunction, and changes will be visible to callers */

/* Don't forget to properly size in and out, e.g. */
void main() {
vector<vector<double> > in, out;
in.resize(3);
out.resize(3);
for(int k=0;k<3;k++) {
in[k].resize(3, 0.0);
out[k].resize(3, 0.0);
}

myFunction(in,out);
}

EDIT: Changed loop iterator from i to k to prevent formatting.

:) Shh, we don't want to scare him to soon with optimization stuff. Generally I just use push_back and call it good on the resizing front. There shouldn't be too much overhead to do it that way.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: blahblah99
You can't return an array declared within a function due to the scope of the variable. As soon as the function returns, any variables declared within the function are destroyed.
Or define the internal array as static

 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: Cogman

:) Shh, we don't want to scare him to soon with optimization stuff. Generally I just use push_back and call it good on the resizing front. There shouldn't be too much overhead to do it that way.

I just wanted to ensure that OP could use the 'provided' code like an array :)