2
#include<stdio.h>

int i = 10;

void func(int *q){

    q = &i;
}

int main(void){

    int i = 20;

    int *p = &i;

    printf("%d\n",*p);

    func(p);

    printf("%d",*p);

    return 0;
}

here value of p in 2nd printf statement should be 10... but when I execute it returns 20 how n why?

William Pursell
  • 204,365
  • 48
  • 270
  • 300
aTechieSmile
  • 139
  • 1
  • 1
  • 11
  • 3
    It shouldn't ..:) – user2736738 Jan 01 '18 at 17:19
  • 1
    To do what you are thinking, you would need `void func (int **q) { *q=&i; }` and call it with `func(&p);` All you are doing is modifying a variable local to `func` – William Pursell Jan 01 '18 at 17:21
  • @coderredoc : but why!! 'coz local variable i will not be considered for the function func(p).. function willl take global i and hence p = &i (global) will be considered so p should be 10... – aTechieSmile Jan 01 '18 at 17:22
  • Possible duplicate of [Initializing a pointer in a separate function in C](https://stackoverflow.com/questions/2486235/initializing-a-pointer-in-a-separate-function-in-c) – Bo Persson Jan 01 '18 at 21:10

3 Answers3

2

It would print 10 if you do this

*q = i

in the called function.

Change the content of that variable whose address is with you in the called function in the local variable q. Don't change the content of the local variable q.

You asked me why? Will explain...

This is what happened when you called the function and printed the value. Well this is not an exhaustive list but this points out the key things that will help in this context.

  1. You got the address of the variable i in p.

  2. Then you pass it to the called function.

  3. Called function now has a local variable whose content is same as that of p.

  4. Now you change the content of that local variable with the address of the global variable i.

  5. Then function ends.

  6. Rest in peace - local variable q. It is dead.

  7. Then you again access *p in main() - meaning you looking for what value there is in the address contained in p.

  8. you get 20.

Is there any other way to get 10?

Well there is

func(&p);

And then you do this

void func(int **q){    
    *q = &i;
}

Now you print *p you will get 10.

Know few things clearly, no matter what C is pass by value. Even here also q is a local variable. But now we have the address of p of main() in q. So when you write *q=&i then you are basically going to the address of p from main() and then write there the address of global variable i. Then you come back. q is again lost. Then you print it - as you change to the original variable (p), the content there is 10.


Code-1

func(p);

then in func()

void func(int *q){
    *q = i;
}

Code-2

func(&p);

then in func()

void func(int **q){
    *q = &i;
}
Community
  • 1
  • 1
user2736738
  • 30,591
  • 5
  • 42
  • 56
2

Here

q = &i

You are setting the value of q, which is a local variable. Changing it does not change the value of the variable passed to func().

To have the 2nd call to printf() print 10, you want to write to where q is pointing.

  *q = i;

q points to the inner i, which this way is set to 10.

alk
  • 69,737
  • 10
  • 105
  • 255
0

here value of p in 2nd printf statement should be 10 (changed) ... but when I execute it returns 20 (same as original).
How and why?

q in void func(int *q) is a copy of p when func(p); is called.

void func(int *q){
    q = &i;
}

int main(void){
    ...
    func(p);
    ...
}

The change q = &i; inside void func(int *q) does not affect p in main().

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256