About the array subsripting operator

K

Kirilenko

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.
 
B

Ben Bacarisse

Kirilenko said:
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))
 
E

Eric Sosman

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.)
 
R

Richard Delorme

Le 30/08/2012 18:53, Ben Bacarisse a écrit :
Kirilenko said:
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...
 
B

Ben Bacarisse

Richard Delorme said:
Le 30/08/2012 18:53, Ben Bacarisse a écrit :
Kirilenko said:
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!
 
J

James Kuyper

Le 30/08/2012 18:53, Ben Bacarisse a écrit :
Kirilenko said:
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.
 
B

Ben Bacarisse

James Kuyper said:
Le 30/08/2012 18:53, Ben Bacarisse a écrit :
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>
 
L

lawrence.jones

Kirilenko said:
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.
 
K

Kaz Kylheku

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.
 
E

Edward A. Falk

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'
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top