+ 3
How to return multi-dimensional arrays from function in C++?
Suppose, I've this task: "write a cpp function to input n coordinates and return it in form of array." And, then I write this function: https://code.sololearn.com/cMsJpPvmg32m/#cpp which gives me two errors: "error: 'giveCoordArray' declared as function returning an array" and "error: 'giveCoordArray' was not declared in this scope" So, I want to ask "what is the correct function-signature or function-definition for a function which returns a two dimensional (or more) array?" And, also what does the above two errors mean?
3 Réponses
+ 4
The error is due to the fact that arrays allocated on the stack (memory reserved by the compiler) is are local to the function they are declared in.
(An array is internally a pointer to the first element and so upon returning the array the callee gets the underlying address. The memory for the element is then deallocated due to end of scope and you get a dangling reference).
A workaround for this would be to allocate memory dynamically using new and return a pointer to pointer (2D array):
int** giveCoordArray()
{
int ** arr = new int[2];
arr[0]=new int[2];
arr[1]=new int[2];
arr[0][0]=x1; arr[0][1]=y1;
// Update other values.
return arr;
}
The only problem is that the pointer must now be deallocated using delete by the programmer. One can also wrap the allocations inside a class's constructor and destructor for simplification.
Another way is to pass the array as an argument instead of returning it, and update values inside the function.
+ 4
The best way, however, would be to use a STL container like std::array ( www.cplusplus.com/reference/array/array ) or std::vector ( www.cplusplus.com/reference/vector/vector ) and return a container of containers.
Eg :
std::array<std::array<int,2>,2> giveCoordArray()
{
// Perform read operations.
std::array<std::array<int,2>,2> res =
{ {
{ x1, y1 },
{ x2, y2 } } };
// 3 sets of braces here.
return res;
}
The benefit of this way is that you need not worry about the allocations for the array, and you get a set of accompanying methods for making things simpler.
Also, in your code, what you are returning is a brace-enclosed initializer list. Those cannot be returned as normal arrays and are used for initialization purposes, though it is possible to accept them as arguments (A function accepting an object of type std::initializer_list -> www.cplusplus.com/reference/initializer_list/initializer_list ).
+ 3
As I know, functions can't actually be arrays. But, functions can be and return pointers, which can be treated as array. In other words, you need a function to be "<type>**" to return a pointer to a 2 or more dimensional array. Something like:
Int** array;
array = new int*[value];
for(i = 0;i<=value;i++)array[i]= new int[value]
This is part of code to make 2d array. This way, you can return it in function and it won't complain.
Or, you can make structure that is actually a 2d array and use it as type of function and return.