If I had the following program:
#include <iostream>
// being lazy and using all std just for example.
using namespace std;
int (*some_func())[10]
{
int arr[10]= {0,1,2,3,4,5,6,7,8,9};
return arr;
}
int main()
{
int arr2[10] = some_func();
for(auto i : arr2)
cout << i << endl;
return 0;
}
is this the correct way to define a function that returns an array? I know I am missing something vital here, my knowledge tells me that when you return arr, you get a *to the first element of arr, but it also tells me that arr is defined locally meaning that arr will be undefined when the function finishes hence when I actually assign arr to arr2 from the return I am assigning it undefined memory which is not what I want, yet if I return &arr isn't that essentially the same?
Any assistance on understanding this in relation to the above example would be useful, It seems I have confused myself somewhere but I'm not quite sure how to rectify it.
Thanks,
Euden
int arr2[10] = some_func();line. And also on thereturn arr;line, together with a warning for good compilers.