简体   繁体   中英

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|

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM