Variable swapping question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chad

    Variable swapping question

    Given something like the following

    void swap(int *x, int *y)
    {
    int temp;

    temp = *x;
    *x = *y;
    *y = temp;
    }

    How does having a temp variable allow for anything to safely swap with
    itself? Like for example x = 10 and y = 10. What would happen in the
    absence of temp?


    Chad
  • Keith Thompson

    #2
    Re: Variable swapping question

    Chad <cdalten@gmail. comwrites:
    Given something like the following
    >
    void swap(int *x, int *y)
    {
    int temp;
    >
    temp = *x;
    *x = *y;
    *y = temp;
    }
    >
    How does having a temp variable allow for anything to safely swap with
    itself? Like for example x = 10 and y = 10.
    I assume you mean *x == 10 and *y == 10.

    But what I suspect you *really* mean is x == y, i.e., x and y both
    point to the same object. The swap function above works just fine in
    that case; the value is saved to temp, then copied over itself, then
    restored from temp. Is there some potential problem you had in mind?
    What would happen in the
    absence of temp?
    Well, you couldn't do the first and third assignments, so it wouldn't
    be able to swap anything. Or did you have something else in mind
    instead of temp? Perhaps something like question 3.3b of the
    comp.lang.c FAQ, <http://c-faq.com/>?

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

    Comment

    • Chad

      #3
      Re: Variable swapping question

      On Jul 4, 6:44 pm, Keith Thompson <ks...@mib.orgw rote:
      Chad <cdal...@gmail. comwrites:
      Given something like the following
      >
      void swap(int *x, int *y)
      {
       int temp;
      >
       temp = *x;
       *x = *y;
       *y = temp;
      }
      >
      How does having a temp variable allow for anything to safely swap with
      itself? Like for example x = 10 and y = 10.
      >
      I assume you mean *x == 10 and *y == 10.
      >
      But what I suspect you *really* mean is x == y, i.e., x and y both
      point to the same object.  The swap function above works just fine in
      that case; the value is saved to temp, then copied over itself, then
      restored from temp.  Is there some potential problem you had in mind?
      >
                                                 What would happen in the
      absence of temp?
      >
      Well, you couldn't do the first and third assignments, so it wouldn't
      be able to swap anything.  Or did you have something else in mind
      instead of temp?  Perhaps something like question 3.3b of the
      comp.lang.c FAQ, <http://c-faq.com/>?
      >
      --
      I was just curious why you needed temp. I did the following

      m-net% more swap.c
      #include <stdio.h>
      void swap(int *x, int *y)
      {
      int temp;


      /*temp = *x ; */
      *x = *y;
      /* *y = temp; */

      }

      int main(void)
      {
      int a = 10;
      int b = 20;

      swap(&a, &b);

      printf("After swap: a=%d , b=%d\n", a, b);

      return 0;
      }
      m-net% ./swap
      After swap: a=20 , b=20
      m-net%

      And now just thought about what you said. Now I need about a hour for
      it to really sink in.

      Comment

      • Ron Ford

        #4
        Re: Variable swapping question

        On Fri, 4 Jul 2008 19:00:47 -0700 (PDT), Chad posted:
        On Jul 4, 6:44 pm, Keith Thompson <ks...@mib.orgw rote:
        >Chad <cdal...@gmail. comwrites:
        >>Given something like the following
        >>
        >>void swap(int *x, int *y)
        >>{
        >> int temp;
        >>
        >> temp = *x;
        >> *x = *y;
        >> *y = temp;
        >>}
        >>
        >>How does having a temp variable allow for anything to safely swap with
        >>itself? Like for example x = 10 and y = 10.
        >>
        >I assume you mean *x == 10 and *y == 10.
        >>
        >But what I suspect you *really* mean is x == y, i.e., x and y both
        >point to the same object.  The swap function above works just fine in
        >that case; the value is saved to temp, then copied over itself, then
        >restored from temp.  Is there some potential problem you had in mind?
        >>
        >>                                            What would happen in the
        >>absence of temp?
        >>
        >Well, you couldn't do the first and third assignments, so it wouldn't
        >be able to swap anything.  Or did you have something else in mind
        >instead of temp?  Perhaps something like question 3.3b of the
        >comp.lang.c FAQ, <http://c-faq.com/>?
        >>
        >--
        >
        I was just curious why you needed temp. I did the following
        >
        m-net% more swap.c
        #include <stdio.h>
        void swap(int *x, int *y)
        {
        int temp;
        >
        >
        /*temp = *x ; */
        *x = *y;
        /* *y = temp; */
        >
        }
        >
        int main(void)
        {
        int a = 10;
        int b = 20;
        >
        swap(&a, &b);
        >
        printf("After swap: a=%d , b=%d\n", a, b);
        >
        return 0;
        }
        m-net% ./swap
        After swap: a=20 , b=20
        m-net%
        >
        And now just thought about what you said. Now I need about a hour for
        it to really sink in.
        I see no mystery in this result; you'll find that this type of swap needs a
        temp. You can swap in other ways, say, using a macro, but the pointer
        version is simply better all around.
        --
        The difference between a moral man and a man of honor is that the latter
        regrets a discreditable act, even when it has worked and he has not been
        caught.
        H. L. Mencken

        Comment

        Working...