How do I swap pointers?

Discussion in 'C' started by Thiengineer, Feb 4, 2007.

  1. Thiengineer

    Thiengineer New Member

    Joined:
    Feb 4, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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!!
     
  2. ganesh_don

    ganesh_don New Member

    Joined:
    Mar 9, 2007
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    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...
     
  3. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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;
    }
    
    
     
  4. DaWeiIsStupid

    DaWeiIsStupid New Member

    Joined:
    Mar 11, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    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;
    }
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice