0

The problem that I'm having is that my pointer that's declared in the main points to the address of a variable that is returned from a function, later I try to pass the pointer in a function that takes in a reference, but the value that it had is now gone and replaced by garbage values somehow. Please show me what I am doing wrong.

#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);
}

0 Answers0