View Single Post
Old 27-03-06, 02:20 PM   #2 (permalink)
harishmitty
Member
 
Join Date: Mar 2006
Location: India
Age: 24
Posts: 51
Thanks: 0
Thanked 1 Times in 1 Posts
Thanks: 0
Thanked 1 Time in 1 Post
Rep Power: 3 harishmitty has disabled reputation


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 ---
harishmitty is offline   Reply With Quote