I’m trying to create a basic function to swap the endianness of a short int but it’s throwing an error:
#ifndef __ENDIAN__
#define __ENDIAN__
#define Swap16(value) \
((((unsigned short)((value) & 0x00FF)) << 8) | \
(((unsigned short)((value) & 0xFF00)) >> 8))
#define Swap32(value) \
((((unsigned)((value) & 0x000000FF)) << 24) | \
(((unsigned)((value) & 0x0000FF00)) << 8) | \
(((unsigned)((value) & 0x00FF0000)) >> 8) | \
(((unsigned)((value) & 0xFF000000)) >> 24))
void __inline SwapEndian(short* value) //ERROR HERE
{
*value = Swap16(value);
}
#endif
I intend to use the code like:
short val = 0x1234;
SwapEndian(&val);
//val now contains 0x3412
I’m using VS2008 and the exact error is:
C2296: '&' : illegal, left operand has type 'short *'
What can I do to fix this error?
If you look at how the macro is expanded it’s pretty clear what’s wrong.
&valis a pointer of typeshort*. But you macro does arithmetic directly off of it’s parameter. So you’re trying to perform integer arithmetic on a pointer.So this gets expanded to:
&valis typeshort*, while the macros expects an integer.In the function you’ll need to dereference it into an integer before passing it in to the macro.