Original Post
Just when I thought I knew everything there was to know about pointers I discover something new. It''s easier to explain if I show some source code first.
After calling modify, I expected num1 to point to the address of t. However, this is not the case, and num1 points to a garbage value after the function call.
I''ve discover 2 ways around this, one way is to do the following: void modify(int *&x);
the second is to have the modify function return the address of t, and set num1 equal to it.
what I''m interested in is why the code above does not work?
digital radiation
void modify(int *x)
{
int *t = new int;
*t = 99;
delete x;
x = t;
}
int main()
{
int *num1 = new int;
*num1 = 256;
modify(num1);
cout << *num1 << endl;
delete num1;
}