compile error

Michael Eager eager@eagerm.com
Wed Jul 18 15:41:00 GMT 2012


On 07/18/2012 07:31 AM, Andrew Haley wrote:
> On 07/18/2012 03:00 PM, ap wrote:
>>
>>
>> I have the following #define in my code
>> #define SWAP64(p) ( (*(unsigned _int64*)(p)) = _byteswap_uint64(*(unsigned _int64*)(p)))
>> When I compile I get the error: expected primary-expression before 'unsigned',
>> when I use SWAP64
>>
>> Does anybody have any idea?
>
> Yes.  This is not legal C code: a cast expression is not an lvalue.
>
> Do this:
>
> #define SWAP64(P) \
>      unsigned _int64 *tmp = (unsigned _int64 *)P; \
>      *tmp = _byteswap_uint64(*tmp);
>
> But be aware that you might have aliasing errors, so this code may
> still not be legal.  I suspect you have some very old C code.

You are also likely to step on any variable named "tmp".

It would be better to re-write _byteswap_uint64() as a macro or
as a function taking two void pointers and do the cast within
the function.


-- 
Michael Eager	 eager@eagercon.com
1960 Park Blvd., Palo Alto, CA 94306  650-325-8077




More information about the Gcc-help mailing list