Brain Teaser

Roderick F MacFarquhar IE87 rmacfarq at cs.strath.ac.uk
Wed Mar 28 20:03: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.

        [code using temp deleted]

>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;

But isn't there is a problem of potential overflow on the x += y instruction.

I think its been given before in this group but how about..

#define SWAP(X,Y) \
	{(X) ^= (Y) ; \
	 (Y) ^= (X) ; \
	 (X) ^= (Y) ;}

>Shawn Oesterle {oesterle at wpi.wpi.edu}

_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
 Roderick MacFarquhar - Information Eng IV, Strathclyde University 
 Janet rmacfarq at cs.strath.ac.uk    Internet rmacfarq%cs.strath.ac.uk at nsf.ac.uk 
 Voice (but I'm rarely home - sob :'{ ) +44 (0)41 339 0263 
 'How many fibres are intertwined in a shreaded wheat biscuit ?' 
~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~"~



More information about the Comp.lang.c mailing list