Use pointer to exchange the values of the two variables - C Function

C examples for Function:Function Parameter

Description

Use pointer to exchange the values of the two variables

Demo Code

#include <stdio.h>
void swap(int *x, int *y);

int main(void)
{
   int i, j;/*  ww w.j  a  va  2  s .co  m*/

   i = 10;
   j = 20;

   printf("i and j before swapping: %d %d\n", i, j);

   swap(&i, &j); /* pass the addresses of i and j */

   printf("i and j after swapping: %d %d\n", i, j);

   return 0;
}

void swap(int *x, int *y)
{
   int temp;
   temp = *x;   /* save the value at address x */
   *x = *y;     /* put y into x */
   *y = temp;   /* put x into y */
}

Related Tutorials