SWAP


The source code for swap.c may be downloaded. The purpose of this program is to demonstrate a procedure which takes parameters passed by value. The two parameters passed to the procedure each point to a variable. After the procedure has been called the values in these two variables have been swapped.

Programs which sort values often make use of "swap" procedures (e.g. sorter).


you type> cat swap.c
void swap(int *a, *b))
       // called with variablest *a,*b;
{       int c;
        c  =*a;
        *a =*b;
        *b =c;
        return;
}
main()
{
        int x,y;
        x = 9;
        y = 7;
        printf("x== %d, and y== %d\n",x,y);
        swap(&x,&y);
        printf("x== %d, and y== %d\n",x,y);
}
you type> cc -o swap swap.c
you type> swap
x== 9, and y== 7
x== 7, and y== 9


BACK to index page.