When you pass a pointer to a function, you can change the thing that it points to and that will be reflected everywhere in the program. But if you change the pointer itself (so it points to something else) then that won't be reflected in the parameter that you pass:
1 2 3 4 5 6 7 8 9 10 11 12
|
int x,y;
void f(int *ip2)
{
ip2 = &y; // ip2 points to y
}
int main()
{
int *ip = &x; // ip points to x
f(ip);
// ip still points to x.
}
|
If you think about this, it's just like any other parameter. Parameters are normally passed by value, whether they are ints, doubles, or pointers.
Now you can change this by passing a parameter by reference:
void f(int &i);
Now any change that you make to i inside f() will also change whatever variable was passed into f.
That syntax works for any parameter type. So if you can pass a pointer by value like this:
void f(int *ip);
then you can a pointer by reference like this:
void f(int * &ip);
Now changes that you make the pointer itself inside f() will also change whatever variable was passed into f.