复合语句是C中的左值(或右值)吗? [英] Are compound statements lvalue (or rvalue) in C?
问题描述
当我检查Linux内核中container_of
宏的定义时,
我看到了复合语句作为宏定义,
When I examined the definition of the container_of
macro in Linux Kernel,
I saw a compound statement as a macro definition,
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
但是,在我看来,不清楚的问题是哪个语句作为右值被考虑在内. 显然最后一条语句的结果用作右值,为什么?
however the unclear issue in my mind is which statement gets taken into account as rvalue. Apparently result of the last statement is used as rvalue, but why?
(type *)( (char *)__mptr - offsetof(type,member) );
例如,下面的代码示例在C语言中是否合法?
For instance, is the code example below legal in C?
int g_count = 0xFF;
#define GLOBAL_COUNT do {g_count;} while(0)
int main(int argc, char *argv[])
{
int local;
local = GLOBAL_COUNT;
local = 0;
GLOBAL_COUNT = local;
return 0;
}
复合语句中变量的分配规则是什么?
What is the assignment rule for variables in compound statements?
推荐答案
({})
是对C的GNU扩展,称为语句表达式:
({})
is a GNU extension to C called a statement expression:
http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
在C中,复合语句是一个语句,不能在表达式中使用该语句.
In C a compound statement is a statement and a statement cannot be used in an expression.
这篇关于复合语句是C中的左值(或右值)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!