[英]C preprocessor Concatenation in a for loop
Is it possible to use concatenation in a for loop? 是否可以在for循环中使用串联? My code snippet is like this:
我的代码段是这样的:
#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);
}
What I want to do in this for loop is like this: 我要在此for循环中执行的操作是这样的:
search(f1, arg1, arg2, tol, max_tries) 搜索(f1,arg1,arg2,tol,max_tries)
search(f2, arg1, arg2, tol, max_tries) 搜索(f2,arg1,arg2,tol,max_tries)
... ...
I know my version is obviously wrong but that's the result I want to archive. 我知道我的版本显然是错误的,但这就是我要存档的结果。
EDIT: 编辑:
I decided not to use macro for this. 我决定不使用宏。
CONCATENATE
creates " fi
", not " f1
". CONCATENATE
创建“ fi
”,而不是“ f1
”。 You want f
to be an array and use i
as an index on it. 您希望
f
为数组,并使用i
作为索引。
Preprocessing is only text replacement which is done before compiling. 预处理仅是文本替换,它是在编译之前完成的。 At this stage, we therefore don't know the value of
int i
. 因此,在此阶段,我们不知道
int i
的值。
If you wish to concatenate the text string "f" and the value of i at runtime, you may do this: 如果您希望在运行时将文本字符串“ f”和i的值连接起来,可以这样做:
char buf[10];
snprintf(buf, 10, "f%i", i);
and then 接着
CALL_SEARCH(buf, ...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.