|
|
On 24 Jul 2000 10:37:25 -0400, Ron Parker wrote:
>On 24 Jul 2000 10:22:27 -0400, Warp wrote:
>> Yes, you can make that code in a #define macro, but the problem is where
>>are you going to the the type for 'tmp'?
>
>Make it one of the parameters to the macro?
Better yet (untested):
#define SWAP(a,b) { \
char tmp[sizeof(a)]; \
assert( sizeof(a) == sizeof(b)); \
memcpy( tmp, &a, sizeof(a)); \
memcpy( &a, &b, sizeof(a)); \
memcpy( &b, tmp, sizeof(a)); \
}
This does assume that your dialect of C allows you to declare variables
at the start of any block rather than just at the top of the function,
but I don't see any way of doing it without stipulating that. It looks
like tmp has a dynamic size, but since sizeof is a compile-time operator,
it really doesn't.
--
Ron Parker http://www2.fwi.com/~parkerr/traces.html
My opinions. Mine. Not anyone else's.
Post a reply to this message
|
|