C 这个内核代码是什么意思?
我在一个旧的内核代码中发现了这一点:C 这个内核代码是什么意思?,c,linux,linux-kernel,C,Linux,Linux Kernel,我在一个旧的内核代码中发现了这一点: #define hlist_for_each_entry(tpos, pos, head, member) \ for (pos = (head)->first; \ pos &&
#define hlist_for_each_entry(tpos, pos, head, member) \
for (pos = (head)->first; \
pos && \
({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
pos = pos->next)
但我不明白该如何解释这句话的含义:
({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;});
编译器是如何理解大括号/圆括号内多个表达式的含义的(
({…;…;})
构造的?似乎这个构造
({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;});
基于编译器自身的语言扩展,允许在表达式中使用复合语句。它与C++中的lambda表达式类似。
我认为施工评估结果具有价值1
在我看来,这只是一个糟糕的代码,因为同样的代码可以使用逗号运算符编写,比如
pos && ( ( tpos = hlist_entry(pos, typeof(*tpos), member) ), 1 ); \
pos = pos->next)
它是一个GCC语言扩展,表达式中的“大括号组”。 正如@nos在对另一个答案的评论中所说的那样,政府对此做出了解释。 表达式
(EXP1;EXP2;…;EXPn;)
的值为EXPn
。
您应该使用其他药物来治疗其副作用——参见以下示例:
#include <stdio.h>
int main() {
int b;
printf("%d\n", ({ b=3; 2;}) );
printf("%d\n",b);
exit(0);
}
似乎使用了编译器的语言扩展,允许使用表达式之类的复合语句
gcc -Wall -pedantic a.c
a.c: In function ‘main’:
a.c:5:17: warning: ISO C forbids braced-groups within expressions [-Wpedantic]
printf("%d\n", ({ b=3; 2;}) );
^