Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   About the array subsripting operator (http://www.velocityreviews.com/forums/t951604-about-the-array-subsripting-operator.html)

Kirilenko 08-30-2012 04:32 PM

About the array subsripting operator
 
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.

Ben Bacarisse 08-30-2012 04:53 PM

Re: About the array subsripting operator
 
Kirilenko <lucas.pesenti2@gmail.com> 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.

Eric Sosman 08-30-2012 04:58 PM

Re: About the array subsripting operator
 
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
esosman@ieee-dot-org.invalid

Richard Delorme 08-31-2012 10:45 AM

Re: About the array subsripting operator
 
Le 30/08/2012 18:53, Ben Bacarisse a écrit :
> Kirilenko <lucas.pesenti2@gmail.com> 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


Ben Bacarisse 08-31-2012 11:00 AM

Re: About the array subsripting operator
 
Richard Delorme <abulmo@nospam.fr> writes:

> Le 30/08/2012 18:53, Ben Bacarisse a écrit :
>> Kirilenko <lucas.pesenti2@gmail.com> 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.

James Kuyper 08-31-2012 11:05 AM

Re: About the array subsripting operator
 
On 08/31/2012 06:45 AM, Richard Delorme wrote:
> Le 30/08/2012 18:53, Ben Bacarisse a écrit :
>> Kirilenko <lucas.pesenti2@gmail.com> 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

Ben Bacarisse 08-31-2012 01:46 PM

Re: About the array subsripting operator
 
James Kuyper <jameskuyper@verizon.net> writes:

> On 08/31/2012 06:45 AM, Richard Delorme wrote:
>> Le 30/08/2012 18:53, Ben Bacarisse a écrit :
>>> Kirilenko <lucas.pesenti2@gmail.com> 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.

lawrence.jones@siemens.com 09-15-2012 09:55 PM

Re: About the array subsripting operator
 
Kirilenko <lucas.pesenti2@gmail.com> 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

Kaz Kylheku 09-15-2012 10:26 PM

Re: About the array subsripting operator
 
On 2012-08-30, Kirilenko <lucas.pesenti2@gmail.com> 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.

Edward A. Falk 09-16-2012 11:43 PM

Re: About the array subsripting operator
 
In article <9104981e-cf45-425b-ad84-c068636086eb@googlegroups.com>,
Kirilenko <lucas.pesenti2@gmail.com> 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, falk@despams.r.us.com
http://thespamdiaries.blogspot.com/


All times are GMT. The time now is 01:20 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.