# Why couldn't I swap the values of two variables with *temp?

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 10-05-2012
whiteflags
Remember that dereferencing c on the left hand side (*c) will overwrite the pointer.

I think you need to realize that you ultimately want to swap values. The temp variable is supposed to save your first value so that when you put the second value where the first value was, you can still put the temp value where the second value was. Your function would work if temp weren't a pointer type.
Code:

```void swap(element *foo, element *bar) {   element temp = *foo;   *foo = *bar;   *bar = temp; }```
That does exactly as I said.

If temp has to be a pointer, there is little point to it, because you have to new the extra space, and delete it before the function ends. The "extra space" part kinda has no way around it.
• 10-05-2012
std10093
Also when i was in your position i found interesting this piece of code.No extra value needed :)
Code:

```void swap(int *a, int *b) {       *a -= *b;       *b += *a; // b gets the original value of a       *a = (*b - *a); // a gets the original value of b }```
• 10-05-2012
laserlight
Interesting, but generally best avoided since the intermediate results can be out of the range of int. The XOR swap has a related idea, but likewise is best avoided as it only works on integral types.
• 10-06-2012
std10093
Quote:

Originally Posted by laserlight
Interesting, but generally best avoided since the intermediate results can be out of the range of int. The XOR swap has a related idea, but likewise is best avoided as it only works on integral types.

Yes,but if someone says now that swap can be done only with an extra variable you can answer this piece of code does not need one :D
• 10-06-2012
laserlight
Quote:

Originally Posted by std10093
Yes,but if someone says now that swap can be done only with an extra variable you can answer this piece of code does not need one

Then when that someone uses input designed to cause your function's computations to overflow/underflow, you will have no words left to say but "oops". Really, if you want to note special cases, go ahead, but in general, what we are talking about is that some extra space is needed for swaps. If you want to contradict, then you should state note that your counterexample is a special case that does not really contribute to this discussion, and is often just trivia that is a poor example to show to a beginner.

(EDIT: and yes, this example is not new to me; I'm sure both whiteflags and I have known of it long before you joined this forum. My caustic reaction here is because it is so typical to bring up such examples without qualifying that it is trivia rather than good practice, and the fact that stahta01 has the temerity to "like" it and you dare to defend it irks me.)
• 10-06-2012
std10093
No intention to contradict :) I did not came up with the code.Someone else showed that to me and i thought that it would be good to share it with the others..I did not want in any case to play the smart guy or something like that :)
And please notice that i wrote some comments,there was nothing else that i was able to come up with to explain the code :)

edit - >Would you like me to delete the code? :)
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12