Thread: swap variable question

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    swap variable question

    say I have something like the following

    Code:
    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? I don't see it.
    Last edited by Overworked_PhD; 07-04-2008 at 06:29 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I assume you meant y to be an int *.

    I'm not sure how much more clearer this can be. temp takes on the value of x. x then takes on the value of y. Finally y takes on the old value of x, stored in temp.

    What are you having trouble with?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Let's say we call swap with x = 10 and y = 20.
    Then we get:

    int temp = 10 (x);
    x = 20 (y);
    y = 10 (temp);

    Doesn't that make sense?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Quote Originally Posted by MacGyver View Post
    I assume you meant y to be an int *.

    I'm not sure how much more clearer this can be. temp takes on the value of x. x then takes on the value of y. Finally y takes on the old value of x, stored in temp.

    What are you having trouble with?
    Yes. It was a typo.

  5. #5
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Quote Originally Posted by MacGyver View Post
    I assume you meant y to be an int *.

    I'm not sure how much more clearer this can be. temp takes on the value of x. x then takes on the value of y. Finally y takes on the old value of x, stored in temp.

    What are you having trouble with?
    I guess I can't picture what happens when the values are equal. Ie x = 10 and y = 10. What would happen in the absence of temp?
    Last edited by Overworked_PhD; 07-04-2008 at 06:34 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Nothing. Since both values are the same, there's no point in swapping them in the first place.
    But if we explore such a path:

    int temp = 10 (x);
    x = 10 (y);
    y = 10 (temp);

    Take out the temp:

    x = 10 (y);
    y = 10 (???);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Imagine you have 3 jars, one empty, one with a spider in it and one with a grasshopper in it.

    You want to swap the spider and the grasshopper (ie they move jars), but they can't go in the same jar together else they'll kill each other. So instead you move the spider to the 3rd emtpy jar, and move the grasshopper into the spider's jar, and the spider into the grasshopper's jar. It doesn't matter if they're both spiders either, they still can't go in the same jar at the same time.


  8. #8
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Quote Originally Posted by zacs7 View Post
    Imagine you have 3 jars, one empty, one with a spider in it and one with a grasshopper in it.

    You want to swap the spider and the grasshopper (ie they move jars), but they can't go in the same jar together else they'll kill each other. So instead you move the spider to the 3rd emtpy jar, and move the grasshopper into the spider's jar, and the spider into the grasshopper's jar. It doesn't matter if they're both spiders either, they still can't go in the same jar at the same time.

    Would this be like saying a pointer object (in C) can't hold more than one value at the same time?

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Like any variable in C... yes

    Of course you could get techincal and argue that, but it will still only have one value, but how you interperate this value is another story. Laymans terms, 5678 you could argue from a programming point of view that 56 and 78 are two values that exist in one variable (and thus unpack them into other variables).

    It really doesn't matter if the values are the same when you swap, it makes no difference at all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Local variable Question
    By WDT in forum C# Programming
    Replies: 2
    Last Post: 04-02-2009, 05:39 PM
  3. Replies: 7
    Last Post: 01-21-2009, 02:27 PM
  4. easy variable question
    By Gav D in forum C Programming
    Replies: 2
    Last Post: 08-28-2003, 06:40 AM
  5. Variable Arguments Question
    By moonwalker in forum C Programming
    Replies: 8
    Last Post: 08-04-2002, 09:08 AM