Hello, i have a simple question about pointers... The code that goes with the problem is:
int* getPtrToArray(int& m)
{
int anArray[5] = { 5, 4, 3, 2, 1 };
m = 5;
return anArray;
}
int main()
{
int n;
int* ptr = getPtrToArray(n);
for (int i = 0; i < 5; i++)
cout << ptr[i] << ' ';
}
What the program is trying to do is simply display "5 4 3 2 1". However, there is a problem within the getPtrToArray function that prevents it from doing this. It asks how to change the getPtrToArray function in order for it to display the numbers correctly. I have a fairly good grasp (i think) on why it doesnt work: basically, since myArray is declared within the getPtrToArray function, it cant pass the whole array back to int main and instead just passes myArray[0] back... My only problem is i have no idea what change i could make to the getPtrToArray function to fix this. I know that if myArray was declared within int main, and the point "ptr" was declared to be equal to myArray, the program would function as intended BUT since im only allowed to change the top function, i dont know what to do really...
Any help would be much appreciated.