I want to define variables in structure based on endianness of machine , This is what I tried
#define IS_BIG_ENDIAN (!(union { uint16_t u16; unsigned char c; }){ .u16 = 1 }.c)
//#define IS_BIG_ENDIAN (!*(unsigned char *)&(uint16_t){1})
struct A {
#if IS_BIG_ENDIAN
char a:4;
char b:4;
#else
char b:4;
char a:4;
#endif
}
The above code is giving error , is this possible to achieve. My Main aim is to achieve reverse order of structure variables.
From https://sourceforge.net/p/predef/wiki/Endianness/
You don't have standard way to have it.
but custom headers <sys/param.h>
or <endian.h>
might provide MACRO such as:
Type Macro Value
Big endian __BYTE_ORDER __BIG_ENDIAN
Little endian __BYTE_ORDER __LITTLE_ENDIAN
Little endian (word-swapped) __BYTE_ORDER __PDP_ENDIAN