0

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|

4

2 回答 2

1

我认为您how在两种意义上使用该名称:作为 的成员cur_tag,以及作为browserTagCheck宏的参数。尝试为宏参数使用不同的名称。

于 2012-10-17T13:35:54.490 回答
0

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.

于 2012-10-17T14:21:22.677 回答