+ 3

What does the swap function mean? How does it work?

void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; }

22nd Oct 2018, 3:51 AM
林克翰
3 ответов
+ 2
instead of swapping original values you are working on the address of those values meaning swapping them using pointers ,so you are actually swapping the values here
23rd Oct 2018, 9:00 PM
Abhay
Abhay - avatar
+ 2
it swaps the values of a and b by using the third variable temp first temp=a then a=b then b=temp first temp got the value of a then a is assign with the new value of b
26th Oct 2018, 2:03 PM
Suraj yadav
Suraj yadav - avatar
0
Abhay, we are working with values of variables. Adress are not swapped. In this case, when we use swap(x, y) adresses of x and y are not changed but values are.
25th Oct 2018, 11:18 AM
Dmytro