我遇到的问题是,在主声明中声明的指针指向从函数返回的变量的地址,后来我尝试将指针传递给带有引用的函数,但是它所拥有的值现在消失了,并以某种方式被垃圾值所取代。请告诉我我在做什么错。
#include<iostream>
void somthingwrong(int &x) //*takes in the pointers address*
{
std::cout << somfing << std::endl; //*supposed to print 5 but outputs a
// garbage value*
return;
}
int& number()
{
int g = 5;
return g;
}
int main()
{
int *h = nullptr;
h = &number(); //*the pointer passed gets the value of 5*
somthingwrong(*h); //*here in this function the value is somehow erased
//and couts some garbage value*
return (0);
}