0
void fun(int *p) 
{ 
  int q = 10; 
  p = &q; 
}     

int main() 
{ 
  int r = 20; 
  int *p = &r; 
  fun(p); 
  printf("%d", *p); 
  return 0; 
}

I have an understanding of pointers, however it was my guess that printf should display 10, although the website's compiler and other compiler say the value remains 20.

Can anyone explain why?

Does fun() have an affect on the pointer p? Why or why not?

MDMoore313
  • 3,233
  • 1
  • 23
  • 38

8 Answers8

2

If p is a function parameter, p = something inside of the function body won't change the value at calling site. It doesn't matter if p is a pointer or not.

You're confusing it with *p = something and p[foo] = something.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
1

The pointer is passed by value, the change is localized to fun(). If you were to change *p that would be seen by main but not the change to the pointer itself.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
1

The variable "p" inside fun() is a local pointer on the stack. You reassign p to point to q (also a local pointer) but when that function exists, the stack frames are popped and the value and the pointer go away.

If you want fun() to actually modify the value of the variable in the calling function, try changing the line:

p = &q;

to be:

*p = q;

This actually writes the value of q (10) to the address pointed to by p, rather than re-assigning p to point to something else.

Joe Hickey
  • 810
  • 7
  • 8
1

You give a copy of the pointer to the function as parameter. So the function cannot change the original.
You probably want to give a pointer to the pointer, which enables the function to alter the pointed-to pointer.

void fun(int **p) 
{ 
  static int q = 10; 
  *p = &q; 
}     

int main() 
{ 
  int r = 20; 
  int *p = &r; 
  fun(&p); 
  printf("%d", *p); 
  return 0; 
}

This however is quite risky, not to say insane.
In my example I changed the local variable to static, in order to avoid the most hideous consequences of returning a pointer to a local variable.
You probably have to redesign on quite a large scope to get a clean solution for whatever you are trying.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • I wouldn't call it insane, the funny part is in the "discussion" section of the website I took this from, they passed a reference to a pointer to the `fun` function, and the fun function took a double pointer as a parameter. It was different than their original question. – MDMoore313 Jan 09 '18 at 12:45
0

the p in the function gets the value of the p in main thus the address of r, then in the function you assign address of q to the p in the function. but the pointer p which is in main is not at all affected by the pointer p in fun thus pointer p in main still has address of variable r. Obviously now you know why it prints 20.

Eternal
  • 928
  • 9
  • 22
0

Your expectation would hold true if your code inside the function was:

*p = q;

This would change the value inside the address pointed by p to the value held by variable q (which is local to the function).

prmottajr
  • 1,816
  • 1
  • 13
  • 22
0

In C, values passed into a function are passed by value; that is, they are copied into the function. The p in fun is a copy of the p in main. This copy is changed, and then discarded as fun returns, leaving the p in main the same.

Incidentally, if it had worked as you expected, printf probably would not have printed 10. The variable q in fun is local, and is destroyed as soon as fun exits. In practice this usually means it’s still in memory, but after the end of the stack*; the call to printf probably overwrites q with some of printf’s parameters or with other local data.


* Actually, since it’s never used except to take its address for p, which in turn is never used, in practice the compiler will optimize it out completely unless you use -O0.

Daniel H
  • 7,223
  • 2
  • 26
  • 41
0

Outside the function fun ( inside main ), the variable 'q' is non existent and therefore its address is not present in p. When you dereference 'p' inside main function it has address of 'r' and not 'q'. If you dereference 'p' inside fun it will have address of 'q' and display 10.