-1

I know that if I pass &a as an argument in fun ,I will get result as *p =20 after fun is executed .Now see in the following code p is pointer variable passed as an argument to function fun. But after compilation I get result as *p =10 instead of 20 .

What is wrong in passing pointer variable p instead of address of a i.e. &a to function fun?

#include <stdio.h>
int a = 10, b = 20;
void fun(int *p)
{
    p = &b;
}
int main()
{
    int *p = &a;
    fun(p);
   //fun(&a);

    printf("%d\n", *p);
    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
AMITk
  • 15
  • 3
  • *"I know that if I pass &a as an argument in fun ,I will get result as `*p =20`"* You need to show us that code, since it must be entirely different than what you've shown in the question. – user3386109 Jul 27 '21 at 17:41
  • In `main`, when you say `int *p = &a; fun(p);`, this does *not change `p`*. – Steve Summit Jul 27 '21 at 17:43
  • @user3386109 done related change in main function but as a comment – AMITk Jul 27 '21 at 17:49
  • If you comment out `fun(p)` and replace it with `fun(&a)`, you'll get 10. – user3386109 Jul 27 '21 at 17:51
  • @user3386109 okay so at the time of calling function with fun(&a) operation , There should be one change inside **fun** function i.e. *p = b instead of p = &b; – AMITk Jul 27 '21 at 17:58
  • Sure, but if you change `p = &b` to `*p = b`, and call the function with `fun(p)`, you'll also get 20. So it has nothing to do with how you call the function, and everything to do with the fact that you changed what the function does. – user3386109 Jul 27 '21 at 18:08

3 Answers3

3

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

The pointer identifiers p in main and fun are not the same, i.e. do not refer to the same object. If in fun() you assign to p, you effectively do nothing. The value of the local parameter p is simply lost upon return.

Jens
  • 69,818
  • 15
  • 125
  • 179
1

The function argument p in fun is a different object in memory from the variable p in main, and updating one does not affect the other.

It appears to work if you pass &a because you initialize main:p to be &a before calling fun. main:p is not being updated at all.

To do what you want, you have to pass a pointer to p:

void fun( int **p )
{
  *p = &b;
}

int main( void )
{
  int *p = &a;
  fun( &p );
  printf( "%d\n", *p );
  return 0;
}
John Bode
  • 119,563
  • 19
  • 122
  • 198