Reputation: 1263
I have a function and want to return a 2d array:
int flip[5][5](int input[5][5]);
But it returns an error: no function error allowed (this is a translation)
How can I fix this?
This works:
void flip(int input[5][5]);
Upvotes: 0
Views: 82
Reputation: 25366
The syntax for returning an array is ugly. You're better off using a typedef, or auto.
auto flip(int input[5][5]) -> int[5][5];
However the problem you have is, you aren't passing the array to the function as a reference or pointer, which won't work.
typedef int myArray[5][5];
void foo(myArray& arr) {
//do stuff
}
int main() {
int arr[5][5];
foo(arr);
return 0;
}
As an aside, working with arrays like this is tedious, you're better off using a container from the STL.
Upvotes: 1
Reputation: 76468
Use std::array<std::array<int, 5>, 5>
instead of a built-in array type.
Upvotes: 1
Reputation: 311108
In this function declaration
void flip(int input[5][5]);
the parameter is implicitly adjusted to pointer to the first element of the array. So actually the function declaration looks like
void flip(int ( *input )[5] );
You may not return an array because arrays do not have a copy constructor.
You may return an array by reference provided that the array itself is not a local variable of the function.
So for example if your funciotn gets an array by reference then you may return this array from the function.
For example
int ( & flip(int ( &input )[5][5]) )[5][5];
You can wrap your array in a structure and return this structure along with the array.
For example
struct data
{
int a[5][5];
};
data flip( int input[5][5] );
Upvotes: 1