why this programme fail to compile
Oct 20, 2010 at 4:27pm UTC
Hi all:
i encounter this somewhat awkward define directive and could not figure out how to resolve this,please help me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <stdio.h>
#include <assert.h>
static unsigned int test_endian_int = 0xdeadbeef;
enum endianess
{
LITTLE,
BIG,
};
#define ENDIANNESS ( *(const char *)&test_endian_int == 0xef ? LITTLE \
: *(const char *)&test_endian_int == 0xde ? BIG \
: assert(0))
int main(int argc, char *argv[])
{
enum endianess this_endian = ENDIANNESS;
if (this_endian == LITTLE) printf("little endian\n" );
return 0;
}
In function 'main':
Line 16: warning: comparison is always false due to limited range of data type
Line 16: warning: comparison is always false due to limited range of data type
Line 16: error: void value not ignored as it ought to be
Thanks in advance!
Oct 20, 2010 at 4:41pm UTC
2 problems:
1) you probalby should be casting to
const unsigned char *
. Since chars are signed and 0xef is unsigned, the comparison will always be false.
2) By putting assert() in your ENDIANNESS macro, there is a possibility that it will have the following effect:
enum endianess = assert(0); // this will happen if neither big or little endian
Since assert returns a void, this is nonsense, hence why you're getting the error.
Topic archived. No new replies allowed.