Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   How do I swap pointers? (http://www.go4expert.com/forums/i-swap-pointers-t2849/)

Thiengineer 5Feb2007 04:53

How do I swap pointers?
 
I need help with the following problem about swapping pointers.

The following code is in main:

Code:

        main()
        {
          int x=1,y=2;
          int *xp,*yp;
          .
          .
          .
          xp = &x;
          yp = &y;
          intpswap(<arg1>,<arg2>);
          x=3;y=4;
          printf("*xp = %d, *yp = %d\n",*xp,*yp);
        }

Write a function called intpswap that swaps what xp and yp point at,
and thus printf prints "*xp = 4, *yp =3".

I know how to swap values by using a temp. variable. Is it the same approach here?

Any help or hints will be greatly appreciated!!

Thank you!!

ganesh_don 14Mar2007 18:03

Re: How do I swap pointers?
 
u should use double pointer in arg1 and arg2 bcoz the swapped address wil get lost when the function returns,rest of the procedure is same...

DaWei 14Mar2007 18:30

Re: How do I swap pointers?
 
Nonsense. He doesn't say he wants to swap the pointers, he says:
Quote:

...that swaps what xp and yp point at,
Code:

#include <stdio.h>

void pswap (int *pa, int *pb)
{
        int temp = *pa;
        *pa = *pb;
        *pb = temp;
        return;
}
int main ()
{
        int a = 1;
        int b = 2;
        int *pa = &a;
        int *pb = &b;

        printf ("a: %d, b: %d\n", a, b);
        pswap (pa, pb);
        printf ("a: %d, b: %d\n", a, b);
        return 0;
}

Quote:

Originally Posted by Output
a: 1, b: 2
a: 2, b: 1
Press any key to continue


DaWeiIsStupid 11Mar2011 15:42

Re: How do I swap pointers?
 
Quote:

Originally Posted by DaWei (Post 9910)
Nonsense. He doesn't say he wants to swap the pointers, he says:

Code:

#include <stdio.h>

void pswap (int *pa, int *pb)
{
        int temp = *pa;
        *pa = *pb;
        *pb = temp;
        return;
}
int main ()
{
        int a = 1;
        int b = 2;
        int *pa = &a;
        int *pb = &b;

        printf ("a: %d, b: %d\n", a, b);
        pswap (pa, pb);
        printf ("a: %d, b: %d\n", a, b);
        return 0;
}


You are stupid.

All you did was swap the values of a and b. pa's value is still the address of a, and pb's value is still the address of b.

OP wants the pointers to swap what they are pointing to which would be done like so

void swapPtr(int ** p1, int ** p2)
{
int *temp = *p1;
*p1 = *p2
*p2 = temp;
}

#define DA_WEI_IS_STUPID 0

int main()
{
...
swapPtr(&p1, &p2);

return DA_WEI_IS_STUPID;
}

xpi0t0s 13Mar2011 00:20

Re: How do I swap pointers?
 
Less of the name calling please. If anyone's stupid it's both of you, because the problem definition is ambiguous (and probably copied wrongly by the OP):

>> Write a function called intpswap that swaps what xp and yp point at,

so it swaps what xp and yp point at, NOT xp and yp, although I suspect from the next part that in fact it does mean to swap the pointers.

>> and thus printf prints "*xp = 4, *yp =3".

Well if you just swapped what xp and yp pointed at, then you would swap the values
int x=1,y=2;
leaving x=2 and y=1, then overwrite those values with
x=3;y=4;

and so
printf("*xp = %d, *yp = %d\n",*xp,*yp);

would then print "*xp=3, *yp=4", NOT "*xp = 4, *yp =3" as per the original problem description.

Therefore it is the POINTERS that the OP wants to swap, not the values themselves.

So the calling code will be
intpswap(&xp,&yp);

where the parameters are both defined as int**, i.e. pointer to (pointer to int).

OP, when you need to modify the values in the caller's context, you must pass by reference not by value. If you pass by value then you pass COPIES of the values, so there is no change in the caller's copy of those variables.


All times are GMT +5.5. The time now is 04:47.