Why can't I pass by pointer
I have a function that takes a pointer as a parameter, this function changes the pointer but when I return from that fuction the pointer is still the same as it was before. Why is that?
int x = 6;
void function( int *n ) // pass in a pointer to an int
{
n = &x; // change the value of the pointer
}
void main ( )
{
int* pa = NULL;
// call the function to change the value of the pointer
function( pa );
// this doesn't work pa = NULL
if( pa )
cout << "Value of pa is " << *pa << endl;
}
I noticed that if I change the definition of function to void function( int *&n ) then it works the way I intend.