user967007
user967007

Reputation:

C preprocessor macro for pointer to structure member

I want to make a C macro to replace an if statement.

I've never been able to get a good grasp of C macros, do I need to do token pasting?

#define BROWSER_HTML_HOW_OPENTAG 0x19939292
structure  { dword how;  } _tag;
structure  { _tag cur_tag;  } _ibot;

// works fine
foo()
{
    _ibot*ibot;
    if(ibot->cur_tag->how==BROWSER_HTML_HOW_OPENTAG) { } // do something
}

but I want to implement this

#define browserTagCheck(tag,how)    (tag->how==how)
foo()
{
    _ibot*ibot;
    if(browserTagCheck(ibot->cur_tag,BROWSER_HTML_HOW_OPENTAG) {} // do something
}

I get an error:

error: expected identifier before numeric constant|

Upvotes: 0

Views: 1491

Answers (2)

Lundin
Lundin

Reputation: 213711

Don't do that. Never try to re-invent the C language, it is very poor practice.

Instead, I would strongly suggest this:

inline bool browserTagCheck (const struct something_t* x)
{
  return x->tag == BROWSER_HTML_HOW_OPENTAG;
}


...

if(browserTagCheck(&ibot))
{
  doStuff();
}

Inlining is most likely not even needed, since this appears to be some sort of web/html app with no real time requirements.

Upvotes: 0

gcbenison
gcbenison

Reputation: 11963

I think you're using the name how in two senses: as a member of cur_tag, and as an argument to your browserTagCheck macro. Try using a different name for the macro argument.

Upvotes: 1

Related Questions