Your swap function is just wrong, the
temp
variable should be use to maintain the original value of
x
.
Try
#include <stdio.h>
void swap(int *x, int *y);
int main()
{
int x, y;
x = 5;
y = 7;
printf("%d and %d \n", x, y);
swap(&x, &y);
printf("swapped: %d and %d \n", x,y);
return 0;
}
void swap(int *x, int *y)
{ int temp = *x;
*x = *y;
*y = temp;
}