funcs_ex1.c (553B)
1 #include <stdio.h> 2 3 /* 4 * Write two swap functions. One using call by value 5 * and one using call by pointer 6 */ 7 8 static void 9 swap_val(int x, int y) 10 { 11 int tmp; 12 13 tmp = x; 14 x = y; 15 y = tmp; 16 } 17 18 static void 19 swap_ptr(int *x, int *y) 20 { 21 int tmp; 22 23 tmp = *x; 24 *x = *y; 25 *y = tmp; 26 } 27 28 int 29 main(int argc, char *argv[]) 30 { 31 int x = 10, y = 20; 32 33 printf("x: %d | y: %d\n", x, y); 34 swap_val(x, y); 35 swap_ptr(&x, &y); 36 printf("x: %d | y: %d\n", x, y); 37 38 return 0; 39 }