Are compound statements an lvalue (or rvalue) in C?

When I looked at the container_of macro definition in the Linux kernel, I saw the compound expression as a macro definition,

 #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) 

however, an unclear problem in my mind is which operator is taken into account as an rvalue. Apparently, the result of the last statement is used as an rvalue, but why?

 (type *)( (char *)__mptr - offsetof(type,member) ); 

For example, the following code example 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 a variable assignment rule in compound statements?

+4
source share
2 answers

({}) is a GNU extension for C called an operator expression:

http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html

In C, a compound statement is an instruction, and the statement cannot be used in an expression.

+8
source

What you see is an expression of an expression, not a compound statement. An operator expression is a GCC extension that does not exist in the standard C language.

In expressions of the GCC expression, the returned results "by value" mean that their results are r values. This actually agrees with the general “philosophy” of the C language: in C, almost any manipulation of lvalue objects causes them to quickly lose their lvalue-ness and turn into rvalues. (In this regard, C is almost the opposite of C ++. C ++ is trying to keep lvalue-ness as long as possible.)

+4
source

All Articles