in Programming in C edited by
8,704 views
1 vote
1 vote
void fun(int *p) 

{

int q = 10;

p = &q;

}

int main()

{

int r = 20;

int *p = &r;

fun(p);

printf("%d", *p);

return 0;

}

 

in Programming in C edited by
8.7k views

1 comment

here no use of function fun().

Output will be 20

right?
0
0

1 Answer

8 votes
8 votes
Best answer

The answer is 20.

because the pointer p in main and pointer p in function are different having different addresses... the p in function is local to the func()

you can refer this:-

selected by
by

4 Comments

it should be

*p = &q;

0
0
yes....changed it....see now..
0
0
void fun(int *p)
{
    printf("op1 = %u\n", p);
 int q = 10;
 p = &q;
 printf("op2 = %u\n", p);
}
int main()
{
 int r = 20;
 int *p = &r;
 fun(p);
 printf("op3 = %u", p);
 return 0;
}

 

i tried something diff and found that here in the op i got the value of op1 and op3 as same and op2 as diff i.e p is not local ..plzz tell how after returning from the fun the value of p is getting changed automatically from op2 to op1??
0
0

Related questions