Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #62166

Re: curly braces inside for loop parameter

From Bartc <bc@freeuk.com>
Newsgroups comp.lang.c
Subject Re: curly braces inside for loop parameter
Date 2015-05-17 16:01 +0100
Organization A noiseless patient Spider
Message-ID <mjaaeu$dn6$1@dont-email.me> (permalink)
References <a6f4eb2e-e20e-41da-b007-db42b48b0421@googlegroups.com>

Show all headers | View raw


On 17/05/2015 14:56, Daniel Doron wrote:
> Hi,
>
> I am working on some stuff involving hlists in kernel. I have seen this in lists.h:
>
> #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)
>
>
> I have never encountered this syntax in C before, I am referring to the curly braces and dangling "1" inside the for loop second parameter.

It looks like a gcc extension which allows statement sequences as 
expressions, by enclosing them in ({...}).

That final 1 is to ensure that the pos && .... condition returns true 
when pos is true.

I've just noticed the whole lot is inside a for-statement, which 
figures! I intensely dislike C's for statement precisely because it can 
be abused like this (and I don't think much of gcc's extension for 
similar reasons).

I *think* the above code is the equivalent of:

    pos = head->first;

    while (pos) {
       tpos = hlist_entry(pos, typeof(*tpos), member);
       pos = pos->next;
    }

-- 
Bartc

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

curly braces inside for loop parameter Daniel Doron <danielmeirdoron@gmail.com> - 2015-05-17 06:56 -0700
  Re: curly braces inside for loop parameter asetofsymbols@gmail.com - 2015-05-17 07:49 -0700
  Re: curly braces inside for loop parameter Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-17 15:52 +0100
  Re: curly braces inside for loop parameter Bartc <bc@freeuk.com> - 2015-05-17 16:01 +0100
    Re: curly braces inside for loop parameter David Kleinecke <dkleinecke@gmail.com> - 2015-05-17 09:14 -0700
      Re: curly braces inside for loop parameter Bartc <bc@freeuk.com> - 2015-05-17 17:43 +0100
    Re: curly braces inside for loop parameter raltbos@xs4all.nl (Richard Bos) - 2015-05-23 10:33 +0000
  Re: curly braces inside for loop parameter Keith Thompson <kst-u@mib.org> - 2015-05-17 12:28 -0700
  curly braces inside for loop parameter Chad <cdalten@gmail.com> - 2015-05-17 13:53 -0700
    Re: curly braces inside for loop parameter gazelle@shell.xmission.com (Kenny McCormack) - 2015-05-17 21:36 +0000

csiph-web