tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 C Language > Basic > Swap using Pointers

Swap using Pointers 

This example shows swap using pointers.

File Name  :  
source/C/basic/swap2.c 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
01#include<stdio.h>
02#include<conio.h>
03 
04void swap(int *a, int *b)
05{
06   *a ^= *b,  *b ^= *a, *a ^= *b;
07}
08 
09int main(int argc, char *argv[]) {
10   int x=2,y=6;    
11 
12   swap(&x,&y);
13   printf("x=%d y=%d",x,y);
14 
15   swap(&x,&y);
16   printf("\nx=%d y=%d",x,y);
17    
18   getch();
19}

It gives the following output,
x=6 y=2
x=2 y=6



 
  


  
bl  br