#include
using namespace std;
int* test(){
int s[ ]={20,12,21,31};
int *ptr{s};
return ptr;
}
int main(){
int *p=test();
cout<<*(p+2); //returns 21
}
The output is 21 but since after function call the memory in which array is stored locally is destroyed, then how does the pointer access it and produce 21 as output??
: and don’t forgot to mark doubt as resolved 