Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unrecognized C macro

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.

like image 653
Chris Avatar asked May 21 '15 20:05

Chris


2 Answers

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)
like image 32
user3629249 Avatar answered Oct 21 '22 14:10

user3629249


To answer your queries,

  1. The -> is a structure pointer dereference operator, used to refer a member variable of a pointer to structure type.

  2. 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 \.

  1. #define MACROs don't return any value. It's considered as a textual replacement in the preprocessing stage.
like image 169
Natasha Dutta Avatar answered Oct 21 '22 13:10

Natasha Dutta