I am new to c++ coding and this question may seem childish to you all. But I am really not able to come up on a solution to my problem. Please help.
#include <iostream>
using namespace std;
int* getnew(int* x){
int temp = *x;
int* y = &temp;
cout << "from function address of y = " << y
<< " and value of y = " << *y << endl;
return y;
}
int main(){
int x = 100;
int* y = getnew(&x);
cout << "address of y = " << y;
cout << " and value of y = " << *y << endl;
return 0;
}
Output of this code :
from function address of y = 0x7ffee52aa914 and value of y = 100
address of y = 0x7ffee52aa914 and value of y = 1
What I thought should be the output :
from function address of y = 0x7ffee52aa914 and value of y = 100
address of y = 0x7ffee52aa914 and value of y = 100
Can someone explain where am I going wrong ?