0

Not Working

#include<stdio.h> void too (int &y){ printf("y = %d\n",y); y=6; printf("y =%d\n",y); } int main() { int x=5; printf("x = %d\n",x); too(x); printf("x = %d\n",x); return 0; }

15th Sep 2019, 6:22 AM
Jordan
Jordan  - avatar
7 Respostas
+ 1
#include<stdio.h> void too (int *y) // need a pointer here { printf("y = %d\n", *y); // print value by dereferencing pointer *y=6; // change pointer value printf("y = %d\n", *y); // print value by dereferencing pointer } int main() { int x = 5; printf("x = %d\n", x); too(&x); // pass the <x> variable address printf("x = %d\n", x); return 0; } This works, please see inline comments 👆
15th Sep 2019, 7:08 AM
Ipang
+ 1
I think u has to procide y as argument not &y
15th Sep 2019, 7:04 AM
Krid Indu
Krid Indu - avatar
+ 1
Jordan then u must use return y in toofunction
15th Sep 2019, 7:08 AM
Krid Indu
Krid Indu - avatar
+ 1
Variable references don't work in C, you must use a pointers.
15th Sep 2019, 3:16 PM
rodwynnejones
rodwynnejones - avatar
0
its answer should be 5 5 6 6
15th Sep 2019, 7:02 AM
Jordan
Jordan  - avatar
0
By writing y O/p: 5 5 6 5 but correct answer is i mentioned above.
15th Sep 2019, 7:06 AM
Jordan
Jordan  - avatar