How to swap two variables without using a third variable & pointer?
Any good reason to do that?
Using temporary variable will be much better.
My guess is you would use the xor operator or use a trick with operator overloading
xor trick which the link is showing has undefined behaviour and the one with += doesn't work for all cases.
If you are using it for educational purposes then there are two options mentioned above by Mr. iamthwee.
And if using for some real application and u think that doing the swap without temporary variable would reduce the space required or the time then do think again. Its not that way. Current compilers are so optimised that using a temp varible doesnt even a fractiion affect the program.
But still if u are so peculiar about performance then usign Assembly Language is the way to go.
So u have two options:
1) use normal temp variable
2) use assembly language procedures
since using XOR and += have their own shortcomings.
> How to swap two variables without using a third variable & pointer?
This ceased to be interesting when we came down from the trees and started using high level programming languages.
If your "teacher" somehow thinks this is a good idea you need a better teacher.
Way too many special cases for it to be considered anything more than a party trick.
Another recent post on the same subject.
Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...
Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...
Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...