How to return an array in C ++

How do I return an array from a function? My code

float ClassArray::arr_sub(float a[100][100], float b[100][100]) { int i,j; for(i = 1; i < 10; i++) { for(j = 1; j < 10; j++){ f[i][j]=b[i][j]-a[i][j]; } } return f; } 

and f returned from this function must be assigned to another array g declared in some other class.

 float g[100][100]; g= cm.arr_sub(T,W); 

but when building classes, he says incompatible type assignment of float to float[100][100] .

0
c ++
source share
4 answers

My answer here to another question about arrays explains why you don't want to use arrays.

As I said in this answer, you cannot assign an array as you try:

 float g[100]; g = foo(); // illegal, assigning to arrays is not allowed 

Another strange limitation on arrays is that you are not allowed to return them from functions:

 float foo()[100]; // illegal, returning an array from a function is not allowed 

Also note that when you declare a function of type float arr_sub(float a[100][100]) , you might think that you are passing an array by value, but in fact it raises one more of the strange exceptions created for arrays. In C and C ++, whenever you declare a formal parameter of a function as an array, the type changes from "array" to "pointer to the type of the element of the array."


Since arrays do not behave as they should, you should use std :: array or std :: vector instead:

 std::array<float,100> foo(); // works std::array<float,100> g; g = foo(); // works 

To make multidimensional arrays you can use:

 std::array<std::array<float,100>,100> g; 

Although this is a bit cumbersome, so you can type it:

 typedef std::array<std::array<float,100>,100> Matrix; Matrix ClassArray::arr_sub(Matrix a, Matrix b) { ... } Matrix g; g = cm.arr_sub(T,W); 

And if you have a compiler that supports C ++ 11, you can even create a template type alias:

 template<typename T,int Rows,int Columns> using Matrix2d = std::array<std::array<T,Columns>,Rows>; Matrix2d<float,100,100> g; 

Performance Note

There is one reason why you might not want to return the value of std :: array by value. If the array is large, then when copying data from the return value to the variable to which you assigned it, there can be a significant execution cost. If this ever turns out to be a problem for you, then the solution with std :: array will be the same as for other large types; use the 'out' parameter instead of returning by value.

 void arr_sub(Matrix a, Matrix b, Matrix &result); Matrix g; arr_sub(T,W,g); 

This does not apply to std :: vector, because std :: vector can use move semantics so as not to copy all its elements.

+11
source share

If you insist on using 2D arrays with simple C, it’s best to pass a pointer to the result along with two input parameters, rather than pass arrays by value the way you did.

However, the best thing to do in C ++ is to use vector<vector<float> > instead and pass it by reference.

 void ClassArray::arr_sub( const vector<vector<float> > &a , const vector<vector<float> > &b , vector<vector<float> > &res) { for(int i=0 ; i != a.size() ; i++) for(int j=0 ; j != b.size() ; j++) res[i][j] = b[i][j] - a[i][j]; } void main() { vector<vector<float> > a(100, vector<float>(100, 12.345)); vector<vector<float> > b(100, vector<float>(100, 98.765)); vector<vector<float> > res(100, vector<float>(100, 0)); arr_sub(a, b, res); } 
+2
source share

The best way to do this is to turn everything into a class. From the sight of things, its Matrix.

There are probably already hundreds of Matrix classes, so it's completely pointless to write another one.

But if it is a training exercise, it can be helpful.

To answer your question, make the third argument to your function: float result[100][100] . Inside your function, write the results to an array of results.

This works because in C and C ++ arrays are always passed by reference and never by value. This is because C ++ only passes a pointer to the beginning of the array.

0
source share

if you really want to return the array, and some how to use it in main (), the most efficient way would be to declare the returned array dynamic. this way you will avoid losing the pointer to this new array, since it will be allocated on the heap, not on the stack.

0
source share

All Articles