swapping of two numbers using pointers

#include <stdio.h>
int main()
{
   int a,b,*p1,*p2,temp;
   scanf("%d%d",&a,&b);
   p1=&a;
   p2=&b;
 
   //one way
  /* temp=*p1;
   *p1=*p2;
   *p2 =temp;*/
 
   //second way
 
  /* *p1=*p1+*p2;
   *p2=*p1-*p2;
   *p1=*p1-*p2;*/
 
   // third way
    *p1=*p1+*p2-(*p2=*p1);
 
   printf("%d %d",*p1,*p2);

   return 0;
}

No comments:

Post a Comment