declaration in for statement

Discussion in 'C Programming' started by Luca Forlizzi, Sep 30, 2010.

  1. The declaration part of a for statement is subject to the constraint
    given in
    Sec. 6.8.5 p3, which says "The declaration part of a for statement
    shall only declare identifiers for objects having
    storage class auto or register."

    DR 277 clarifies that this constraint is violated if the declaration
    declare an identifier that
    is not an object, like a tag or an enumeration constant.

    To me (as a non-native english speaker) it is not clear if the
    constraint is violated
    in the case where the declaration declares a new type without a tag
    (if the new
    type has a tag, then it clearly violates the constraint because the
    tag is an
    identifier not denoting an object), like for instance in:

    for (struct { short a[2]; float b; } d = { {0, 0}, 0 }; d.b<10.5; d.b+
    +) ...

    I notice that gcc silently accept this construct, while producing
    diagnostics in
    cases where is present an identifier not denoting an object with auto
    or register
    storage class.
    Is gcc right?
     
    Luca Forlizzi, Sep 30, 2010
    #1
    1. Advertising

  2. Luca Forlizzi

    spygg Guest

    On Oct 1, 12:50 am, Luca Forlizzi <> wrote:
    > The declaration part of a for statement is subject to the constraint
    > given in
    > Sec. 6.8.5 p3, which says "The declaration part of a for statement
    > shall only declare identifiers for objects having
    > storage class auto or register."
    >
    > DR 277 clarifies that this constraint is violated if the declaration
    > declare an identifier that
    > is not an object, like a tag or an enumeration constant.
    >
    > To me (as a non-native english speaker) it is not clear if the
    > constraint is violated
    > in the case where the declaration declares a new type without a tag
    > (if the new
    > type has a tag, then it clearly violates the constraint because the
    > tag is an
    > identifier not denoting an object), like for instance in:
    >
    >         for (struct { short a[2]; float b; } d = { {0, 0}, 0 }; d.b<10.5; d.b+
    > +) ...
    >
    > I notice that gcc silently accept this construct, while producing
    > diagnostics in
    > cases where is present an identifier not denoting an object with auto
    > or register
    > storage class.
    > Is gcc right?


    According to my eexperience " struct { short a[2]; float b; } d =
    { {0, 0}, 0 }; " is just a expression its vaule (as a whole)is 1 or
    0
    PS:I am new learner ,just give my opinion and wait for C cattle man^_^
     
    spygg, Oct 1, 2010
    #2
    1. Advertising

  3. spygg <> writes:

    > On Oct 1, 12:50 am, Luca Forlizzi <> wrote:
    >> The declaration part of a for statement is subject to the constraint
    >> given in
    >> Sec. 6.8.5 p3, which says "The declaration part of a for statement
    >> shall only declare identifiers for objects having
    >> storage class auto or register."
    >>
    >> DR 277 clarifies that this constraint is violated if the declaration
    >> declare an identifier that
    >> is not an object, like a tag or an enumeration constant.


    I was going to leave this alone, but since I am commenting on a comment...

    >> To me (as a non-native english speaker) it is not clear if the
    >> constraint is violated
    >> in the case where the declaration declares a new type without a tag
    >> (if the new
    >> type has a tag, then it clearly violates the constraint because the
    >> tag is an
    >> identifier not denoting an object), like for instance in:
    >>
    >>         for (struct { short a[2]; float b; } d = { {0, 0}, 0 }; d.b<10.5; d.b+
    >> +) ...


    I'd say the common interpretation is that any declaration of anything
    but an identifier for an object (of the right storage class) is a
    constraint violation. The key, then, is what is declared by that 'for'
    statement?

    There is some ambiguity in the meaning of the word "declare". It would
    be nice if identifiers were always declared and objects, types and
    function were defined. This is a distinction that is common when
    talking about programming languages in an abstract way, but it seems
    that the C standard does not want to use this distinction. Some
    sections talk about declaring is structure type with or without a tag.

    The question then hangs on whether 'struct { int i; } d;' declares
    anything other than the identifier d. From the language used in, say,
    6.2.7 p1 I'd have to say that this declares a structure type as well and
    so it does not "only declare identifiers for objects...".

    Part of me would like to "correct" the standard and say that 'struct {
    int i; } d;' *declares* the identifier 'd' and also *defines* a structure
    type at the same time. That's how I'd talk about it, but the meaning of
    the word 'declare' is important for 6.8.5 p3 and it seems it includes
    what I would casually call a structure type definition.

    I put "correct" in quotes because there is nothing wrong with the usage
    in the standard. In particular it follows from the fact the a
    "declaration" is syntax name that covers everything from function
    definitions to external object declarations.

    >> I notice that gcc silently accept this construct, while producing
    >> diagnostics in
    >> cases where is present an identifier not denoting an object with auto
    >> or register
    >> storage class.
    >> Is gcc right?

    >
    > According to my eexperience " struct { short a[2]; float b; } d =
    > { {0, 0}, 0 }; " is just a expression its vaule (as a whole)is 1 or
    > 0


    It's a declaration, not an expression. As for the 0 or 1 I don't see
    how you come to that conclusion. Are you confusing = with ==?

    > PS:I am new learner ,just give my opinion and wait for C cattle man^_^


    --
    Ben.
     
    Ben Bacarisse, Oct 1, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noah
    Replies:
    5
    Views:
    960
  2. Alex Vinokur
    Replies:
    4
    Views:
    592
    Jonathan Turkanis
    Apr 5, 2004
  3. Ovidesvideo
    Replies:
    4
    Views:
    517
    Andrey Tarasevich
    Dec 10, 2004
  4. kelvSYC
    Replies:
    6
    Views:
    7,234
    Richard Herring
    May 17, 2005
  5. Replies:
    4
    Views:
    1,081
    Richard Tobin
    Dec 12, 2006
Loading...

Share This Page