Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #62156 > unrolled thread
| Started by | Daniel Doron <danielmeirdoron@gmail.com> |
|---|---|
| First post | 2015-05-17 06:56 -0700 |
| Last post | 2015-05-17 21:36 +0000 |
| Articles | 10 — 9 participants |
Back to article view | Back to comp.lang.c
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
| From | Daniel Doron <danielmeirdoron@gmail.com> |
|---|---|
| Date | 2015-05-17 06:56 -0700 |
| Subject | curly braces inside for loop parameter |
| Message-ID | <a6f4eb2e-e20e-41da-b007-db42b48b0421@googlegroups.com> |
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.
Can someone explain this?
Thanks,
Daniel.
[toc] | [next] | [standalone]
| From | asetofsymbols@gmail.com |
|---|---|
| Date | 2015-05-17 07:49 -0700 |
| Message-ID | <1cffb6f8-288f-43cb-8c83-39b97fd1bb04@googlegroups.com> |
| In reply to | #62156 |
1; would be right in C
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-05-17 15:52 +0100 |
| Message-ID | <87bnhjmf24.fsf@bsb.me.uk> |
| In reply to | #62156 |
ram@zedat.fu-berlin.de (Stefan Ram) writes:
> Daniel Doron <danielmeirdoron@gmail.com> writes:
>>#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)
>>Can someone explain this?
>
> 1st, one can legally #define anything, the RHS does not have to be C.
>
> 2nd, a compound literal (6.5.2.5) does not seem to be possible here,
> it must be a GNU extension. One can guess its meaning from the »1;«.
It's not the right syntax for a compound literal. It's a GNU extension
-- an expression formed from a block statement (as you say, guessable
from the 1). Odd because it's not needed here. A plain
(tpos = hlist_entry(pos, typeof(*tpos), member), 1)
would do.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Bartc <bc@freeuk.com> |
|---|---|
| Date | 2015-05-17 16:01 +0100 |
| Message-ID | <mjaaeu$dn6$1@dont-email.me> |
| In reply to | #62156 |
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
[toc] | [prev] | [next] | [standalone]
| From | David Kleinecke <dkleinecke@gmail.com> |
|---|---|
| Date | 2015-05-17 09:14 -0700 |
| Message-ID | <5417011d-a001-40db-8a0a-dc7bad31c0ef@googlegroups.com> |
| In reply to | #62166 |
On Sunday, May 17, 2015 at 8:02:01 AM UTC-7, Bart wrote:
> 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
I said I prefer to never use "for" statements and got soundly spanked.
It sounds to me like you agree with me
[toc] | [prev] | [next] | [standalone]
| From | Bartc <bc@freeuk.com> |
|---|---|
| Date | 2015-05-17 17:43 +0100 |
| Message-ID | <mjagd4$431$1@dont-email.me> |
| In reply to | #62173 |
On 17/05/2015 17:14, David Kleinecke wrote: > On Sunday, May 17, 2015 at 8:02:01 AM UTC-7, Bart wrote: >> 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 > I said I prefer to never use "for" statements and got soundly spanked. > It sounds to me like you agree with me Possibly for different reasons. I like ordinary for-statements, but not the way they are implemented in C, which makes simple iterative loops fiddly to write, and encourages people to use them in ways that aren't appropriate, as in the OP. -- Bartc
[toc] | [prev] | [next] | [standalone]
| From | raltbos@xs4all.nl (Richard Bos) |
|---|---|
| Date | 2015-05-23 10:33 +0000 |
| Message-ID | <55605727.8975140@news.xs4all.nl> |
| In reply to | #62166 |
Bartc <bc@freeuk.com> wrote:
> On 17/05/2015 14:56, Daniel Doron wrote:
> > #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)
> 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).
Whereas I think the real problem _is_ the extension and its abuse, not
the for statement. This code would, after all, be just as confusing if
it used a while instead of a for loop.
Richard
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-05-17 12:28 -0700 |
| Message-ID | <87d21z9f6u.fsf@kst-u.example.com> |
| In reply to | #62156 |
Daniel Doron <danielmeirdoron@gmail.com> writes:
> 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. Can someone explain this?
This:
( { ... } )
is called a "statement expression". It doesn't exist in standard
C, so any code that uses it is not portable. It's a gcc-specific
extension (presumably also implemented by other compilers that
emulate gcc).
The feature is documented here:
https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/Statement-Exprs.html
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Chad <cdalten@gmail.com> |
|---|---|
| Date | 2015-05-17 13:53 -0700 |
| Message-ID | <bc2a6a8d-5f20-4040-b5e8-66efed8a65a5@googlegroups.com> |
| In reply to | #62156 |
What's a hlist?
[toc] | [prev] | [next] | [standalone]
| From | gazelle@shell.xmission.com (Kenny McCormack) |
|---|---|
| Date | 2015-05-17 21:36 +0000 |
| Message-ID | <mjb1lg$ulv$1@news.xmission.com> |
| In reply to | #62201 |
In article <hlist-20150517231423@ram.dialup.fu-berlin.de>,
Stefan Ram <ram@zedat.fu-berlin.de> wrote:
>Chad <cdalten@gmail.com> writes:
>>What's a hlist?
>
> »hlist« is an identifier. See: 6.4.2 Identifiers.
>
Sorta like:
Q: What's a rose?
A: "Rose" is a word used to refer to certain things in the English language.
(See: Section of dictionary that defines "word")
--
Genesis 2:7 And the LORD God formed man of the dust of the ground, and
breathed into his nostrils the breath of life; and man became a living soul.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web