【发布时间】:2025-12-26 06:35:12
【问题描述】:
当我检查 Linux 内核中 container_of 宏的定义时,
我看到复合语句是一个宏定义,
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
但是,在我看来,不清楚的问题是哪个语句被视为右值。 显然最后一条语句的结果被用作右值,但是为什么呢?
(type *)( (char *)__mptr - offsetof(type,member) );
例如,下面的代码示例在 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;
}
复合语句中变量的赋值规则是什么?
【问题讨论】:
标签: c compound-assignment