7

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))) ?

5
  • 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. Commented Aug 30, 2012 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. Commented Aug 30, 2012 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. Commented Aug 30, 2012 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. Commented Aug 30, 2012 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. Commented Aug 30, 2012 at 18:48

1 Answer 1

11

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.