//* Passing by pointer compared to passing by reference *

#include <stdio.h> void SwapBad(int a, int b) { int t; t = a; a = b; b = t; } /* *** does not exist in C *** void SwapRef(int &a, int &b) { int t; t = a; a = b; b = t; } */ void SwapPtr(int *a, int *b) { int t; t = *a; *a = *b; *b = t; } int main() { int x, y, z; printf("Please enter three values to start testing\n"); scanf("%d %d %d", &x, &y, &z); printf("E : %d %d %d\n", x, y, z); SwapBad(x,y); printf("SB1: %d %d %d\n", x, y, z); SwapBad(y,z); printf("SB2: %d %d %d\n", x, y, z); /* *** does not exist in C *** SwapRef(x,y); printf("SR1: %d %d %d\n", x, y, z); SwapRef(y,z); printf("SR2: %d %d %d\n", x, y, z); */ SwapPtr(&x,&y); // use & when * is used function-side!!! printf("SP1: %d %d %d\n", x, y, z); SwapPtr(&y,&z); printf("SP2: %d %d %d\n", x, y, z); return(0); }