如何使用C预处理程序连接字符串并在其中加点? [英] How to concatenate strings with C preprocessor with dots in them?
问题描述
我已经阅读了以下问题,答案似乎很明确:
如何连接两次使用C预处理程序,然后按 arg ## _ ## MACRO中的宏进行扩展?
I've read the following question and the answer seems clear enough: How to concatenate twice with the C preprocessor and expand a macro as in "arg ## _ ## MACRO"?
但是如果VARIABLE在结束?
But what if VARIABLE has a dot at the end?
我正在尝试创建一个简单的宏,该宏可在结构体中递增计数器以进行调试。即使没有上述问题的帮助,我也可以轻松地做到这一点,只需
I'm trying to do a simple macro that increments counters in a struct for debugging purposes. I can easily do this even without the help from the above question simply with
#ifdef DEBUG
#define DEBUG_INC_COUNTER(x) x++
#endif
并称呼它
DEBUG_INC_COUNT(debugObj.var1);
但添加 debugObj。对每个宏来说似乎都是多余的。但是,如果我尝试串联:
But adding "debugObj." to every macro seems awfully redundant. However if I try to concatenate:
#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
所以我应该如何更改它,以便
So how should I change this so that
DEBUG_INC_COUNTER(var);
生成
debugObj.var++;
?
推荐答案
忽略 ##
;仅在要连接字符串时才需要。由于参数不是字符串,因此它们之间的空格无关紧要( debugObj。var1
与 debugObj.var1 $ c相同$ c>)。
Omit the ##
; this is only necessary if you want to join strings. Since the arguments aren't strings, the spaces between them don't matter (debugObj . var1
is the same as debugObj.var1
).
这篇关于如何使用C预处理程序连接字符串并在其中加点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!