The function parameter p
void fun(int *p)
{
p = &b;
}
is a local variable of the function. The function deals with a copy of the value of the pointer passed to the function
fun(p);
So within the function it is its local variable that is changed.
You may imagine the function and its call the following way (I will rename the function parameter to ptr
for clarity)
fun(p);
//...
void fun( /* int *ptr */ )
{
int *ptr = p;
ptr = &b;
}
As you can see the original pointer p
was not changed.
If you want to change the pointer you need to pass it by reference. In this case the function will look like
void fun( int **p)
{
*p = &b;
}
and in main it is called like
fun( &p );
In this case the pointer p
(and not a copy of its value) will be indeed changed in the function.
That is the general rule is if you want to change an object in a function you nned to pass it to the function by reference. In C passing by reference means passing an object indirectly through a pointer to it. In this case dereferencing the pointer you will get a direct access to the original object.