Monday 26 May 2014

How to swap 2 numbers using pointers.

#iclude"stdio.h"
#include"conio.h"
void main()
{
int a=4,b=8;
void swap(int *,int *); // Prototype  for the function
clrscr();
printf("\nNumbers before swap : a = % d, b = %d",a,b); // print variable value before swap
swap(&a,&b);
printf("\nNumbers after swap : a = % d, b = %d",a,b); / print variable value after swap
getch();
 }
void swap(int *p,int *q)
{
int t; // Temporary variable to store the value before swapping
t=*p;
*p=*q;
*q=t;
}

No comments:

Post a Comment