我读过以下问题及答案似乎很清楚就够了: How to concatenate twice with the C preprocessor and expand a macro as in "arg ## _ ## MACRO"?如何连接字符串与C预处理器与点在其中?
但是,如果变量在最后一个点?
我正在尝试做一个简单的宏,用于调试目的在结构中增加计数器。我可以很容易地做到这一点,即使没有上述问题的帮助下简单地
#ifdef DEBUG
#define DEBUG_INC_COUNTER(x) x++
#endif
,并调用它
DEBUG_INC_COUNT(debugObj.var1);
但添加“debugObj。”对每一个宏都显得多余。但是,如果我再做连接:
#define VARIABLE debugObj.
#define PASTER(x,y) x ## y++
#define EVALUATOR(x,y) PASTER(x,y)
#define DEBUG_INC_COUNTER(x) EVALUATOR(VARIABLE, x)
DEBUG_INC_COUNTER(var)
gcc -E macro.c
我得到
macro.c:6:1: error: pasting "." and "var" does not give a valid preprocessing token
所以我应该如何改变这种做法,
DEBUG_INC_COUNTER(var);
产生
debugObj.var++;
?
D'oh!我在那里太聪明了。 – Makis 2012-04-10 08:58:23