I've come across a macro defined in a C header file that I'm having a little bit of trouble understanding.
#if BAR
#define FOO(s,err) \
((SOMEPOINTER)(s))->VALID != SOMEVARIABLE \
? (err) \
:
#else
#define FOO(s,err)
and that's it. I understand what's going on with the if
/else
, but I'm not sure what the first macro definition of FOO
is doing. There's obviously a ternary operation going on, but I'm curious about the ->
as I can't find references to it online. I'm also curious about the fact that there don't seem to be any return values here. What's the point of doing a comparison if you're not returning anything regardless of the outcome? To be honest, the "\" guys in there are freaking me out a bit too.
if the name 'BAR' is defined to the pre processor of the compiler
then
define a macro 'FOO( s, err )'
with the replacement text:
"((SOMEPOINTER)(s))->VALID != SOMEVARIABLE ? (err) :"
which is a ternary operator with a missing final parameter
(so it would use what ever statement follows the macro invocation
else, when the name 'BAR' is not known to the pre processor of the compiler
then
define the macro 'FOO( s, err )' as 'nothing
(so what ever statement follows the macro invocation will always be executed,
rather than conditionally executed (as it would be above)
To answer your queries,
The ->
is a structure pointer dereference operator, used to refer a member variable of a pointer to structure type.
The \
is used to write multi-line MACROS. In reference to C11
standard, chapter §6.10.3,
The parameters are specified by the optional list of identifiers, whose scope extends from their declaration in the identifier list until the new-line character that terminates the
#define
preprocessing directive.
So, to span a definition of a MACRO over multiple line, you need to use the \
.
#define
MACROs don't return any value. It's considered as a textual replacement in the preprocessing stage.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With