when passing a=4, the given functions returns :
int* temp1(int a)
{
int b = a*2;
return &b;
}
int* temp2(int a)
{
int b = a*2;
int *p = &b;
return p;
}
int main()
{
cout << *temp1(4); // error
cout << *temp2(4); // output = 8
}
Why these above two functions have different behaviour? Whereas, the below have same outputs?
int a = 3;
cout << *(&a); // 3
and
int a= 3;
int *p = &a;
cout << *p; // 3