1 votes 1 votes void fun(int *p) { int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); printf("%d", *p); return 0; } Programming in C programming-in-c barc2016 pointers + – bad_engineer asked Mar 13, 2016 edited Dec 9, 2023 by Hira Thakur bad_engineer 8.7k views answer comment Share Follow See 1 comment See all 1 1 comment reply srestha commented Mar 14, 2016 reply Follow Share here no use of function fun(). Output will be 20 right? 0 votes 0 votes Please log in or register to add a comment.
Best answer 8 votes 8 votes 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:- Nishu answered Mar 14, 2016 selected Mar 15, 2016 by Himanshu1 Nishu comment Share Follow See all 5 Comments See all 5 5 Comments reply Show 2 previous comments viv696 commented Mar 15, 2016 reply Follow Share it should be *p = &q; 0 votes 0 votes Nishu commented Mar 15, 2016 reply Follow Share yes....changed it....see now.. 0 votes 0 votes deviljee commented Dec 27, 2019 reply Follow Share 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 votes 0 votes Please log in or register to add a comment.