Using pointers to make swapping work - C Pointer

C examples for Pointer:Address

Description

Using pointers to make swapping work

Demo Code

#include <stdio.h> 
  
void interchange(int * u, int * v); 
   /*  w ww .j  a v a  2s.co m*/
int main(void) 
{ 
    int x = 5, y = 10; 
   
    printf("Originally x = %d and y = %d.\n", x, y); 
    interchange(&x, &y);  // send addresses to function 
    printf("Now x = %d and y = %d.\n", x, y); 
   
    return 0; 
} 
   
void interchange(int * u, int * v) 
{ 
    int temp; 
   
    temp = *u;       // temp gets value that u points to 
    *u = *v; 
    *v = temp; 
}    

Result


Related Tutorials