0

can anyone explain me why the values are not swapped in the below code ??

main()
{
int *i,*j;
*i=4000;
*j=9000;
f(i,j);
printf(" %d %d",*i,*j); //output =4000 9000
}

void f( int *a, int *b)
{
int *temp;
temp=a;
a=b;
b=temp;
}

3
Contributors
5
Replies
6
Views
11 Years
Discussion Span
Last Post by Salem
1

Because you're just swapping addresses, not values. You want to dereference the pointers:

void f( int *a, int *b)
{
  int temp;
  temp=*a;
  *a=*b;
  *b=temp;
}
0

i got your answer to how swap pointers ...
But can u tell me what's happening in the code i have written ???
is the address which is passed by i & j is copied in pointers a & b and only adress among a & b is swapped ... so no effect on i & j ..is this what happening ???
can u explain me details what actually happening in my code ...

0

> can anyone explain me why the values are not swapped in the below code ??
Another problem is that your pointers are uninitialised - who knows, maybe they both point at the same memory location.

0

> can anyone explain me why the values are not swapped in the below code ??
Another problem is that your pointers are uninitialised - who knows, maybe they both point at the same memory location.

i have work with the same code after initializing it ..but there is no change in output ...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.