Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!mcsun!hp4nl!kunivv1!atcmpe!jc From: jc@atcmp.nl (Jan Christiaan van Winkel) Newsgroups: comp.lang.c Subject: Re: Brain Teaser Message-ID: <578@atcmpe.atcmp.nl> Date: 28 Mar 90 07:58:22 GMT References: <10289@wpi.wpi.edu> Organization: AT Computing, Nijmegen, The Netherlands Lines: 27 From article <10289@wpi.wpi.edu>, by oesterle@wpi.wpi.edu (Shawn H. Oesterle): ! Problem: ! Swap two pointers without using a third pointer. ! Example: ! { ! void * x, * y, * tmp; ! tmp = x; ! x = y; ! y = x; ! } ! ! Make a piece of code which has the same effect as this, but without ! using the 'tmp' variable (in C, of course). The problem is that pointers cannot be used in arithmatic operations without the danger for inportability. Supposing x and y are ints (or longs), you can do the following: x=x^y; y=x^y; x=x^y; /* swap x and y */ casting the pointers x and y to a long and v.v. is your responsability... JC -- ============================================================================= Jan Christiaan van Winkel Tel: +31 80 566880 jc@atcmp.nl AT Computing P.O. Box 1428 6501 BK Nijmegen The Netherlands =============================================================================