Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: About the array subsripting operator

37 views
Skip to first unread message

Eric Sosman

unread,
Aug 30, 2012, 12:58:26 PM8/30/12
to
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) [i]
not
which ? array1 : array2 [i]

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
eso...@ieee-dot-org.invalid

lawrenc...@siemens.com

unread,
Aug 31, 2012, 2:18:06 PM8/31/12
to
On 8/30/2012 12:32 PM, Kirilenko wrote:
>
> 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 were added (way back in C99!) in response to a
comment from the UK that it would be clearer that way.
--
Larry Jones

I obey the letter of the law, if not the spirit. -- Calvin
0 new messages