1

I want to know processor byte order. I wrote macros. And give problem operator: '*' has no left operand. Please help me.

#define ORDER_LE (*(uint16_t*)("\0\1") >> 8)
#define ORDER_BE (*(uint16_t*)("\1\0") >> 8)
paulsm4
  • 114,292
  • 17
  • 138
  • 190
Penguin
  • 101
  • 7
  • Please show a [mre]. – Ted Lyngmo Apr 13 '23 at 17:49
  • Because this code depends on alignment and aliasing issues that are not guaranteed by any standard, it will always be platform-specific. What platform(s) is/are this intended for? (Isn't x86 the only platform on which this code is safe because it doesn't require 2-byte alignment for 2-byte integers? And, if so, why not just define `ORDER_LE` to 1 and `ORDER_BE` to 0?) – David Schwartz Apr 13 '23 at 17:50
  • 1
    Preprocessor doesn't have a byte order, since it can't execute anything dependent on byte order. Normally it just replaces text. How do you use those macros? In an `#if`? – HolyBlackCat Apr 13 '23 at 17:53
  • 2
    It's not unreasonable to use a C macro to determine byte order. Look at the options in [this thread](https://stackoverflow.com/questions/2100331/macro-definition-to-determine-big-endian-or-little-endian-machine), or use a (compiler-specific) macro like `__BYTE_ORDER`: https://sourceforge.net/p/predef/wiki/Endianness/ – paulsm4 Apr 13 '23 at 18:00
  • 1
    The error message you present makes sense only if you are using those macros in a context where the preprocessor needs to evaluate their expansion text -- that is, in the condition of a preprocessor `#if` or `#elif` directive. Such an expression needs, after macro expansion and evaluation of `defined` operators, to be an *integer constant expression*. String literals and pointers cannot appear in these. As a result, I don't think there is any standard way for the preprocessor to compute endianness. – John Bollinger Apr 13 '23 at 21:18

0 Answers0