1. 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.

2. 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. 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?

4. Originally Posted by MacGyver
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. Originally Posted by MacGyver
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?

6. 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 (???);

7. 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. Originally Posted by zacs7
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. 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.