Brain Teaser

Steve Harrold swh at hpcupt1.HP.COM
Thu Mar 29 02:43:20 AEST 1990


>>> Problem:
>>>	Swap two pointers without using a third pointer.

Answer:
	The sequence:

		x ^= y ;
		y ^= x ;
		x ^= y ;

	will exchange the contents of two like-sized values, "x" and "y".

To answer your specific question, you will probably have to coerce your
pointer variables to int (or long) to make this sequence work.

Of course, once you do that, you no longer have portable code!!.

Do you have a practical need to bypass the use of a temporary variable?
If this is just an intellectual exercise, then post some more.  I think
many of us would enjoy them.



More information about the Comp.lang.c mailing list