Brain Teaser

Lucio de Re lucio at proxima.UUCP
Sat Apr 7 21:55:03 AEST 1990


In article <10289 at wpi.wpi.edu> oesterle at wpi.wpi.edu (Shawn H. Oesterle) writes:
>Problem:
>        Swap two pointers without using a third pointer.
>Hint:
>        Two numbers may be swaped without using any other memory by executing
>        the following statements:
> 
>        int x, y;       /* or you can chose double, long, etc. */
> 
>        x += y;
>        y = x - y;
>        x -= y;

Well, I'd never have thought of it, thanks. Might be useful when
programming the i8051. As for the pointers swap, I suppose you
can't do the same because you need an integer in the middle. Maybe
a cast will save you, but is it worth it (or portable?).

----------------------------------------------------------------------
I used to design nuclear reactors and you should see the code that
engineers and scientists come up with. Yuuuck! Spaghetti everywhere.
-------------------------------------------------- (Kenneth L Moore) -
Lucio de Re                        ...uunet!ddsw1!olsa99!proxima!lucio
-------------------------------------------------------- lucio at proxima
                Disclaimer: He must have been joking!



More information about the Comp.lang.c mailing list