#include // This will swap local variables named x and y, but will not affect // the values of variables in the main function int swap_f (int x, int y) { int t; t = x; x = y; y = t; return 0; } // This will swap the values pointed to by x and y. // The addresses themselves are not swapped. int swap (int *x, int *y) { int t; t = *x; *x = *y; *y = t; return 0; } int main () { int a = 3, b = 5; printf("Before swap a = %d, b = %d \n", a, b); swap_f(a, b); printf("After swap a = %d, b = %d \n", a, b); printf("Before pointer-based swap a = %d, b = %d \n", a, b); swap(&a,& b); printf("After pointer-based swap a = %d, b = %d \n", a, b); int *ap = (int*) malloc( sizeof(int) ); int *bp = (int*) malloc( sizeof(int) ); *ap = 3; *ab = 5; printf("Before pointer-based swap a = %d, b = %d \n", *ap, *bp); swap(ap,bp); printf("After pointer-based swap a = %d, b = %d \n", *ap, *bp); return 0; }