for循环中的C预处理器级联
可以在for循环中使用串联吗?我的代码片段如下所示:for循环中的C预处理器级联,c,macros,concatenation,c-preprocessor,C,Macros,Concatenation,C Preprocessor,可以在for循环中使用串联吗?我的代码片段如下所示: #define CONCATE(a, b) a ## b #define CALL_SEARCH(n, x, y) search(n, arg1, arg2, x, y) ... int i; for (i = 1; i (less than or equal to) number; ++i) { results = CALL_SEARCH(CONCATE(f, i), tol, max_tries); } 在这个for循环中,我要
#define CONCATE(a, b) a ## b
#define CALL_SEARCH(n, x, y) search(n, arg1, arg2, x, y)
...
int i;
for (i = 1; i (less than or equal to) number; ++i)
{
results = CALL_SEARCH(CONCATE(f, i), tol, max_tries);
}
在这个for循环中,我要做的是:
#define CONCATE(a, b) a ## b
#define CALL_SEARCH(n, x, y) search(n, arg1, arg2, x, y)
...
int i;
for (i = 1; i (less than or equal to) number; ++i)
{
results = CALL_SEARCH(CONCATE(f, i), tol, max_tries);
}
搜索(f1、arg1、arg2、tol、最大尝试次数)
搜索(f2、arg1、arg2、tol、最大尝试次数)
我知道我的版本显然是错误的,但这就是我想要存档的结果
编辑:
我决定不为此使用宏。
连接
创建“fi
”,而不是“f1
”。您希望f
成为一个数组,并使用i
作为其索引。预处理是编译之前完成的唯一文本替换。因此,在这个阶段,我们不知道inti
的值
如果希望在运行时连接文本字符串“f”和i的值,可以执行以下操作:
char buf[10];
snprintf(buf, 10, "f%i", i);
然后
CALL_SEARCH(buf, ...)
为什么不直接使用一个数组
f
?看看哪个数组实际上也可以移植到C。