# About the array subsripting operator

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

1. ### KirilenkoGuest

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

2. ### Ben BacarisseGuest

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

3. ### Eric SosmanGuest

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
4. ### Richard DelormeGuest

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
5. ### Ben BacarisseGuest

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
6. ### James KuyperGuest

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
7. ### Ben BacarisseGuest

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
8. ### 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
--
Larry Jones

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

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
10. ### Edward A. FalkGuest

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