Swapping two variables in place

Theodore A. Camus tac at cs.brown.edu
Mon Sep 24 04:29:19 AEST 1990


>There was a message a while back about not being able to create a good
>#define function to swap two variables.
>
>How about this one: #define swap(a,b) a^=b^=a^=b

>But it won't work on `float's and `double's, as `^' is defined only on
>integral types.


Nor would it work for swap(i,A[i]).  
In fact, the usual { tmp = i ;  i = A[i] ; A[i] = tmp }  
would fail too.  Thus the danger of call-by-name.  

[ref 2nd Dragon book]



  CSnet:     tac at cs.brown.edu                          Ted Camus  
  ARPAnet:   tac%cs.brown.edu at relay.cs.net             Box 1910 CS Dept
  BITnet:    tac at browncs.BITNET                        Brown University
  "An ounce of example is worth a pound of theory."    Providence, RI 02912



More information about the Comp.lang.c mailing list