6

Quoting from the C11 standard :

Array subsripting (§ 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 the brackets around E1 necessary (they were missing in the C89 standard), ie in which expression can (*(E1+(E2))) be different from (*((E1)+(E2))) ?

Community
  • 1
  • 1
md5
  • 23,373
  • 3
  • 44
  • 93
  • My only guess is the former means E2 is evaluated before E1. The latter does not give precedence and the parenthesis are equally nested. No idea why. – Doug T. Aug 30 '12 at 16:16
  • 9
    I didn't write that line, or any line in the standard, but my GUESS is that "E1" and "E2" are meant to be expressions, and they just wanted to emphasize that E1 gets completely evaluated even when you basically "copy & paste" it into the description. – Christian Stieber Aug 30 '12 at 16:19
  • I completely aggree with @ChristianStieber. In adition you have to have in mind that `+` and `[]` have different priorities, so if `E1` is an expression it could bind differently to the `+` behind. – Jens Gustedt Aug 30 '12 at 16:30
  • 1
    @JensGustedt: The fact that the source has been parsed as `E1[E2]` proves that however `E1` is composed, its parts are bound at least as tightly as the subscript operator. That implies it binds more tightly than `+`, so how could `E1+(E2)` differ from `(E1)+(E2)`? Yet the change from 1989 to 1999 suggests there is some reason. This is a good question. – Eric Postpischil Aug 30 '12 at 17:10
  • 1
    Note that the editorial change was made to C99; it's not new with C11 which might be suggested by the question and the tags. – Michael Burr Aug 30 '12 at 18:48

1 Answers1

10

According to http://www.open-std.org/jtc1/sc22/wg14/www/docs/n841.htm, it is inserted just for clarity. The two expressions are the syntatically equivalent.

Public Comment Number PC-UK0103
Comment 1.
Category: Editorial change/non-normative contribution
Committee Draft subsection: 6.3.2.1
Title: Array subscripting example
Detailed description:

Paragraph 2 should replace "(*(E1+(E2)))" by "(*((E1)+(E2)))", to avoid confusion. Yes, I know that the syntactic chart makes it quite unambiguous, but the current wording in paragraph 2 is very confusing.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005