+ 3
What does the swap function mean? How does it work?
void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; }
3 Answers
+ 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
+ 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
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.