|
Iamgiving 2 versions of the same program ..
// Version-1: In this the swapped value(modified value)can be seen in the main method
#include<stdio.h>
#include<conio.h>
void swap(int *a,int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void main()
{
int *p = 10;
int *q = 20;
clrscr();
printf("\n BEFORE SWAPPING :");
printf(" P = %d \t Q = %d",p,q);
swap(&p,&q);
printf("\n AFTER SWAPPING :");
printf(" P = %d \t Q = %d",p,q);
getch();
}
//Version 2: In this swapped value is not reflected in main function
#include<stdio.h>
#include<conio.h>
void swap(int *a,int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void main()
{
int *p = 10;
int *q = 20;
clrscr();
printf("\n BEFORE SWAPPING :");
printf(" P = %d \t Q = %d",p,q);
swap(p,q);
printf("\n AFTER SWAPPING :");
printf(" P = %d \t Q = %d",p,q);
getch();
}
Watsthe diff b/w the two versions is ....
In version 1 , Iam sending address of the pointer to the function swap() as arguments .. Hence the pointers p & q are modified directly reflecting changed values when i print their values in main function..
In version 2,Iam just sending the pointers reference .. Hence the modified values aren't reflected in main function ..
Regards,
HARISH MITTY
__________________
HARISH MITTY
--- Life is a PUZZLE ---
|