Using Pointers to Exchange Values - C Pointer

C examples for Pointer:Pointer Variable

Description

Using Pointers to Exchange Values

Demo Code

#include <stdio.h>

void exchange (int * const pint1, int * const pint2){
    int temp;//from   w  w  w  .  j  av  a  2  s .  c o m

    temp = *pint1;
    *pint1 = *pint2;
    *pint2 = temp;
}

int main (void)
{
    int i1 = -5, i2 = 6, *p1 = &i1, *p2 = &i2;

    printf ("i1 = %i, i2 = %i\n", i1, i2);

    exchange (p1, p2);
    printf ("i1 = %i, i2 = %i\n", i1, i2);

    exchange (&i1, &i2);
    printf ("i1 = %i, i2 = %i\n", i1, i2);

    return 0;
}

Result


Related Tutorials