About the array subsripting operator

Discussion in 'C Programming' started by Kirilenko, Aug 30, 2012.

  1. Kirilenko

    Kirilenko Guest

    Hey c.l.c. !

    I was reading the C11 standard (§6.5.2.1) :

    > The definition of the subscript operator [] is that E1[E2] is identical to
    > (*((E1)+(E2))).


    I would like to know why are brackets around E1 necessary (they were missing
    in the C89 standard), ie in which case can (*(E1+(E2))) be different from
    (*((E1)+(E2))) ?

    Thank you,
    L.P.
    Kirilenko, Aug 30, 2012
    #1
    1. Advertising

  2. Kirilenko <> writes:

    > I was reading the C11 standard (§6.5.2.1) :
    >
    >> The definition of the subscript operator [] is that E1[E2] is identical to
    >> (*((E1)+(E2))).

    >
    > I would like to know why are brackets around E1 necessary (they were missing
    > in the C89 standard), ie in which case can (*(E1+(E2))) be different from
    > (*((E1)+(E2))) ?


    When E1 uses (at the top or outer level) any operator with lower
    precedence than +. For example,

    *(x=y+(E2)) is not the same as *((x=y)+(E2))

    --
    Ben.
    Ben Bacarisse, Aug 30, 2012
    #2
    1. Advertising

  3. Kirilenko

    Eric Sosman Guest

    On 8/30/2012 12:32 PM, Kirilenko wrote:
    > Hey c.l.c. !
    >
    > I was reading the C11 standard (§6.5.2.1) :
    >
    >> The definition of the subscript operator [] is that E1[E2] is identical to
    >> (*((E1)+(E2))).

    >
    > I would like to know why are brackets around E1 necessary (they were missing
    > in the C89 standard), ie in which case can (*(E1+(E2))) be different from
    > (*((E1)+(E2))) ?


    The parentheses would only make a difference if E1 were an
    expression involving operators of lower precedence than `+'.
    But any such operator would also have lower prececence than `[]',
    so E1 would already need to be parenthesized:

    (which ? array1 : array2)
    not
    which ? array1 : array2

    Perhaps the extra parentheses were added for clarity's sake, in
    the fear that someone might think E1 in the first line above
    was the "bare" `which ? array1 : array2', which would make hash
    of the stated equivalence.

    (Questions about "Why is the Standard written in thus-and-such
    a way?" will probably do better in comp.std.c than in comp.lang.c.
    CC'ed, and follow-ups set.)

    --
    Eric Sosman
    d
    Eric Sosman, Aug 30, 2012
    #3
  4. Le 30/08/2012 18:53, Ben Bacarisse a écrit :
    > Kirilenko <> writes:
    >
    >> I was reading the C11 standard (§6.5.2.1) :
    >>
    >>> The definition of the subscript operator [] is that E1[E2] is identical to
    >>> (*((E1)+(E2))).

    >>
    >> I would like to know why are brackets around E1 necessary (they were missing
    >> in the C89 standard), ie in which case can (*(E1+(E2))) be different from
    >> (*((E1)+(E2))) ?

    >
    > When E1 uses (at the top or outer level) any operator with lower
    > precedence than +. For example,
    >
    > *(x=y+(E2)) is not the same as *((x=y)+(E2))
    >


    If I understand well: x=y[E2] is identical to (*((x=y)+(E2))).

    Hmmm... I will stay away from C11 for some time then...

    --
    Richard
    Richard Delorme, Aug 31, 2012
    #4
  5. Richard Delorme <> writes:

    > Le 30/08/2012 18:53, Ben Bacarisse a écrit :
    >> Kirilenko <> writes:
    >>
    >>> I was reading the C11 standard (§6.5.2.1) :
    >>>
    >>>> The definition of the subscript operator [] is that E1[E2] is identical to
    >>>> (*((E1)+(E2))).
    >>>
    >>> I would like to know why are brackets around E1 necessary (they were missing
    >>> in the C89 standard), ie in which case can (*(E1+(E2))) be different from
    >>> (*((E1)+(E2))) ?

    >>
    >> When E1 uses (at the top or outer level) any operator with lower
    >> precedence than +. For example,
    >>
    >> *(x=y+(E2)) is not the same as *((x=y)+(E2))
    >>

    >
    > If I understand well: x=y[E2] is identical to (*((x=y)+(E2))).


    No, x=y[E2] is an assignment (at the top level) setting x to an element
    of y. (*((x=y)+(E2))) is a de-reference. It is the same as (x=y)[E2].

    > Hmmm... I will stay away from C11 for some time then...


    C11 has not changed the meaning of any of these constructs!

    --
    Ben.
    Ben Bacarisse, Aug 31, 2012
    #5
  6. Kirilenko

    James Kuyper Guest

    On 08/31/2012 06:45 AM, Richard Delorme wrote:
    > Le 30/08/2012 18:53, Ben Bacarisse a écrit :
    >> Kirilenko <> writes:
    >>
    >>> I was reading the C11 standard (§6.5.2.1) :
    >>>
    >>>> The definition of the subscript operator [] is that E1[E2] is identical to
    >>>> (*((E1)+(E2))).
    >>>
    >>> I would like to know why are brackets around E1 necessary (they were missing
    >>> in the C89 standard), ie in which case can (*(E1+(E2))) be different from
    >>> (*((E1)+(E2))) ?

    >>
    >> When E1 uses (at the top or outer level) any operator with lower
    >> precedence than +. For example,
    >>
    >> *(x=y+(E2)) is not the same as *((x=y)+(E2))
    >>

    >
    > If I understand well: x=y[E2] is identical to (*((x=y)+(E2))).


    The relevant grammar rules require that

    x=y[E2]

    be parsed equivalently to

    x = ( y [ E2 ] )

    This fact is often described by saying that subscripting has higher
    precedence than assignment. Therefore, for purposes of 6.5.2.1, E1 is
    'y', not 'x=y', and the application of that rule therefore generates:

    x = ( * ( ( y ) + ( E2 ) ) )

    Therefore, Ben's example doesn't really work. Eric covered this issue
    correctly.

    > Hmmm... I will stay away from C11 for some time then...


    This is nothing new in C11; it's just a subtle mistake on Ben's part.
    --
    James Kuyper
    James Kuyper, Aug 31, 2012
    #6
  7. James Kuyper <> writes:

    > On 08/31/2012 06:45 AM, Richard Delorme wrote:
    >> Le 30/08/2012 18:53, Ben Bacarisse a écrit :
    >>> Kirilenko <> writes:
    >>>
    >>>> I was reading the C11 standard (§6.5.2.1) :
    >>>>
    >>>>> The definition of the subscript operator [] is that E1[E2] is identical to
    >>>>> (*((E1)+(E2))).
    >>>>
    >>>> I would like to know why are brackets around E1 necessary (they were missing
    >>>> in the C89 standard), ie in which case can (*(E1+(E2))) be different from
    >>>> (*((E1)+(E2))) ?
    >>>
    >>> When E1 uses (at the top or outer level) any operator with lower
    >>> precedence than +. For example,
    >>>
    >>> *(x=y+(E2)) is not the same as *((x=y)+(E2))
    >>>

    >>
    >> If I understand well: x=y[E2] is identical to (*((x=y)+(E2))).

    >
    > The relevant grammar rules require that
    >
    > x=y[E2]
    >
    > be parsed equivalently to
    >
    > x = ( y [ E2 ] )
    >
    > This fact is often described by saying that subscripting has higher
    > precedence than assignment. Therefore, for purposes of 6.5.2.1, E1 is
    > 'y', not 'x=y', and the application of that rule therefore generates:
    >
    > x = ( * ( ( y ) + ( E2 ) ) )
    >
    > Therefore, Ben's example doesn't really work. Eric covered this issue
    > correctly.


    I work fine as an answer to the question that was asked. I thought of
    mentioning the same point Eric made, but then I decided that it might
    confuse the issue since I was responding to the specific question:

    "in which case can (*(E1+(E2))) be different from (*((E1)+(E2)))?"

    Much simpler, I thought, just to answer that! Oh well...

    <snip>
    --
    Ben.
    Ben Bacarisse, Aug 31, 2012
    #7
  8. Kirilenko

    Guest

    Kirilenko <> wrote:
    > Hey c.l.c. !
    >
    > I was reading the C11 standard (?6.5.2.1) :
    >
    > > The definition of the subscript operator [] is that E1[E2] is identical to
    > > (*((E1)+(E2))).

    >
    > I would like to know why are brackets around E1 necessary (they were missing
    > in the C89 standard), ie in which case can (*(E1+(E2))) be different from
    > (*((E1)+(E2))) ?


    They're not necessary (they can never be different in this particular
    case), but you have to go through the syntax pretty carefully to
    determine that and so people thought their being missing was a mistake.
    Only time will tell whether having them there raises more questions than
    it answers.
    --
    Larry Jones

    I like Mom to be impressed when I fulfill the least of my obligations.
    -- Calvin
    , Sep 15, 2012
    #8
  9. Kirilenko

    Kaz Kylheku Guest

    On 2012-08-30, Kirilenko <> wrote:
    > Hey c.l.c. !
    >
    > I was reading the C11 standard (§6.5.2.1) :
    >
    >> The definition of the subscript operator [] is that E1[E2] is identical to
    >> (*((E1)+(E2))).

    >
    > I would like to know why are brackets around E1 necessary (they were missing
    > in the C89 standard), ie in which case can (*(E1+(E2))) be different from
    > (*((E1)+(E2))) ?


    There is no such case. E1 and E2 are primary expressions, being identifiers.
    Therefore, the following suffices: *(E1 + E2).

    There is no suggestion anywhere that this is macro replacement text in which E1
    and E2 are parameters denoting naive token sequences.

    So either you take themn literally (they are primary expressions), or else
    you interpret these as meta-syntax: i.e. that they denote subordinate
    expressions which are proper children of the + operator.

    Meta-syntax works at the abstract syntax tree level, not as a macro
    preprocessor. That is obvious to anyone who has a shred of intellect.
    Kaz Kylheku, Sep 15, 2012
    #9
  10. In article <>,
    Kirilenko <> wrote:
    >Hey c.l.c. !
    >
    >I was reading the C11 standard (§6.5.2.1) :
    >
    >> The definition of the subscript operator [] is that E1[E2] is identical to
    >> (*((E1)+(E2))).

    >
    >I would like to know why are brackets around E1 necessary (they were missing
    >in the C89 standard), ie in which case can (*(E1+(E2))) be different from
    >(*((E1)+(E2))) ?
    >
    >Thank you,
    >L.P.


    Today's fun fact:

    5["abcdefghi"]

    is perfectly valid C syntax. It evaluates to 'f'

    --
    -Ed Falk,
    http://thespamdiaries.blogspot.com/
    Edward A. Falk, Sep 17, 2012
    #10
    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. Jakob Bieling

    Q: operator void* or operator bool?

    Jakob Bieling, Mar 5, 2004, in forum: C++
    Replies:
    2
    Views:
    578
    Rob Williscroft
    Mar 5, 2004
  2. Myth__Buster
    Replies:
    13
    Views:
    370
    Anand Hariharan
    Feb 26, 2013
  3. Myth__Buster
    Replies:
    0
    Views:
    248
    Myth__Buster
    Feb 1, 2013
  4. Myth__Buster
    Replies:
    1
    Views:
    217
    Myth__Buster
    Feb 1, 2013
  5. Myth__Buster
    Replies:
    0
    Views:
    237
    Myth__Buster
    Feb 1, 2013
Loading...

Share This Page