3

I've noticed that gcc accepts code like:

if ( ({ int ret; /* code here */; ret }) == some_value)
    ...;

for ( i = ({ int ret; /* code here */; ret }); i < top; i++)
    ...;

and so on.
One can create an anonymous codeblock and treat it as rvalue (as long as the last statement is a variable/value).

This is useful in some situations - the particular use-case I've found is to return values from inline assembly statements this way. But it'd also be possible to use this to convert functions returning void but modifying memory through pointer arguments, like:

void myfunc(int *arg) { *arg = ...; }

if ( ({int ret; myfunc(&ret); ret; }) == 54321) { ... };

I've been trying to find documentation that this is *normal8 / that the C standard allows for this kind of thing, poor man's lambda.

Can anyone provide details ?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
FrankH.
  • 17,675
  • 3
  • 44
  • 63
  • Actually ... a bit more searching, this is a dup of http://stackoverflow.com/questions/1238016/are-compund-statements-blocks-surrounded-by-parens-expressions-in-ansi-c – FrankH. Apr 02 '14 at 11:30
  • 1
    Only your vocabulary is wrong, they are used as rvalues, here. – Jens Gustedt Apr 02 '14 at 12:35

0 Answers0