Image of my question: https://i.sstatic.net/x5VQW.png
To summarise, in one function, readNumbers, I will make an array of size 10, and return a pointer to this array. I then have a second function, printNumbers, that takes the pointer from the above function (and the length of the array, 10) and prints the index and corresponding value in the array.
My function file:
#include <iostream>
using namespace std;
int *readNumbers()
{
int arr[10];
for (int i=0; i<10; i++) //asking for 10 values
{
cout<<"Enter an element: ";
cin>>arr[i];
}
int *arrPtr;
arrPtr=&arr[0];
return arrPtr;
}
void printNumbers(int *numbers,int length)
{
for (int i=0; i<length; i++)
{
cout<<i<<" "<<*(numbers+1)<<endl;
}
delete numbers;
}
Main file:
int *readNumbers() ;
void printNumbers(int *numbers,int length) ;
int main (void)
{
printNumbers(readNumbers(), 10) ;
}
The code compiles, however, the elements of the array printed are not what has been fed into the compiler.
Also, the question days to delete the array. Is this related to clearing the heap space? Is this achieved by deleting the pointer to the array, as I have done in the second function?
Thank you.
std::array<int, 10>orstd::vector<int>.