+ 1

What is the problem in the code?

swap two values using function by reference method , https://code.sololearn.com/csfvq2IfQ2K7/?ref=app

11th Nov 2018, 6:20 PM
Abhay
Abhay - avatar
4 Answers
+ 6
Nothing. Just in printf should be *a and *b for printing the values and not the addresses.
11th Nov 2018, 6:25 PM
Anya
Anya - avatar
+ 4
"but if I print in definition part then I don't have to use pointer a and b" void swap(int*a,int*b) { int t; t=*b; *b=*a; *a=t; printf("after swapping\n%d and %d",a,b); // Output: 2358860 and 2358856 (decimal represemtation of where a and b point to) printf("after swapping\n%d and %d", *a, *b); // Sample Output: 5 and 2 (for input 2 and 5) } So, they still need to get dereferenced through indirection (asterisk) operator.
11th Nov 2018, 7:07 PM
Babak
Babak - avatar
0
ah I see thks ,but if I print in definition part then I don't have to use pointer a and b
11th Nov 2018, 6:29 PM
Abhay
Abhay - avatar
0
yea I see
11th Nov 2018, 7:18 PM
Abhay
Abhay - avatar