#include <stdio.h>
void swap(int *p1, int *p2) {
// Your logic here
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
int main() {
int a, b;
scanf("%d %d", &a, &b);
swap(&a, &b);
printf("a = %d ", a);
printf("b = %d", b);
return 0;
}
✅ Step-by-Step Algorithm
ptr1
and ptr2
temp
temp = *ptr1
(store value at ptr1
)*ptr1 = *ptr2
(copy value from ptr2
to ptr1
)*ptr2 = temp
(restore original ptr1
value into ptr2
)✅ Step-by-Step Algorithm (Using XOR)
ptr1
and ptr2
ptr1 != ptr2
(to avoid zeroing the value if both point to same location)*ptr1 = *ptr1 ^ *ptr2
*ptr2 = *ptr1 ^ *ptr2
*ptr1 = *ptr1 ^ *ptr2
Input
10 20
Expected Output
a = 20 b = 10