what is the output of this code and why?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

what is the output of this code and why??

void fun(int *ptr) {*ptr=30;} int main(){ int y=20; fun(&y); printf("%d",y); return 0;} https://code.sololearn.com/cu35vsqJl483

21st Jun 2022, 6:11 AM
Nariman Tajari
Nariman Tajari - avatar
3 Answers
+ 2
Line 5 passes the address of variable y to function fun Line 2 changes the contents of this address to 30 The contents of the address of variable y is the variable itself - so changing it changes the variable value Line 6 prints the changed y
21st Jun 2022, 9:21 AM
Emerson Prado
Emerson Prado - avatar
0
it is 30 as the compiler and sololearn site says but i m confused why the value of it is like this and not 20.
21st Jun 2022, 6:18 AM
Nariman Tajari
Nariman Tajari - avatar
0
Main: Hey lets save 20 in box y of our post office.(int y=20) Hey fun my old friend, look the post office, i put something in the green box(fun(&y)) Fun: Cool box, whatever was in there before, lets change it to 30 packets(*ptr=30) Main: lets count the packets in our box and shout it…”30”
21st Jun 2022, 9:54 AM
Alexander Thiem
Alexander Thiem - avatar