SWAP macro

sbanner1 sbanner1 at uvicctr.UUCP
Thu Jun 26 02:42:22 AEST 1986


In article <1577 at brl-smoke.ARPA> Schauble at MIT-MULTICS.ARPA (Paul Schauble) writes:
>I've gotten lots of respones to my request for a form of the macro that
>would make
>  swap(*a++, *b++);
> work.  Almost all of them missed the point.  The swap requires a
>temporary.  Generating that temporary requires knowing the type.
>Several of the solutions would work fine if *a and *b were int's, but I
>never said that.
>
>Recall the my original message was posted as a justification for a
>typeof macro, akin to sizeof.  I find that the need to generate
>temporaries in a macro is not uncommon.  Unless one is willing to do a
>different version of the macro for each type, you need a way to generate
>a new variable of the same type as one of the macro args.  The language
>provides no way to do this.

I don't think you were reading all of the replys too closely.  While
I can see the use of the 'typeof' operator [not macro], there is a 
relatively easy way to do this, as atleast one person mentioned :

    #define swap(type, a, b)  {type temp ... }

Granted this can get a bit ugly if you will need three or four
types, or if they are complex, but the facility *IS* there.

Disclamer : The veiws expressed here may not be shared my anyone else
on the face of the earth.

                               S. John Banner

!uw-beaver!uvicctr!sbanner1   :UUCP
ccsjb at uvvm   :Bitnet



More information about the Comp.lang.c mailing list