Where can I put the array index?

Possible duplicate:
In C arrays, why is this so? a [5] == 5 [a]

This question asks why

a[5] == 5[a] 

They answer in all aspects except one ...

Why is it allowed to put the index of an array after an integer in the first place? And why it’s not allowed to write something like

 [a]5 

or

 [5]a 

or put [] in another odd place?

In other words, what is the definition of where the array index operator is allowed?

EDIT I: The answers I received lead that the standard was a little hard to understand. But with the help of the respondents, I now understand. Array index (square bracket) is allowed after a pointer or integer. If it follows the pointer, then there must be an integer inside the brackets. If it follows an integer, then there must be a pointer inside the brackets.

I accept a less responsive answer because it made a little more effort to understand what I understood from the standard. But the answer, which is strictly quoted by the standard, is also correct. At first it was harder to understand.

EDIT II: I don't think my question was a duplicate. My question concerned a valid grammar regarding an array index operator. This was answered by quotes from the standard, which never appear in the question, which I supposedly duplicated. It looks like yes, but not a duplicate.

+4
source share
3 answers

Postfix grammar of an expression from the C11 standard:

 postfix-expression: primary-expression postfix-expression [ expression ] postfix-expression ( argument-expression-listopt ) postfix-expression . identifier postfix-expression -> identifier postfix-expression ++ postfix-expression -- ( type-name ) { initializer-list } ( type-name ) { initializer-list , } 

Grammar of the primary expression from the C11 standard:

 primary-expression: identifier constant string-literal ( expression ) generic-selection 

And so on. 5 is an integer constant, therefore 5 [a] correspond to this:

 postfix-expression [ expression ] 

Hope this is what you mean.

EDIT: I forgot to mention this, but other comments have already been made:

One of the expressions must have a type pointer '' to complete the type of the object, and the other expression must have an integer type, and the result is of type type.

In this "integer type" he needed to prohibit indexing of floating point constants.

+6
source

Defined in the sub-heading operator section of the standard C array here:

(C99, 6.5.2.1p2) "The postfix expression followed by the expression in square brackets [] is the indexed designation of the element of the array object. The definition of the index operator [] is that E1 [E2] matches (* ((E1 ) + (E2))).

and regarding the permitted types E1 and E2 :

(C99, 6.5.2.1p1) "One of the expressions must be of type" "a pointer to the type of the object, the other expression must be of integer type, and the result is of type" ".

+6
source

a[5] translates to *(a+5) . Addition is commutative, therefore a+5 = 5+a , which can be transferred back to 5[a] . I agree that this is a useless feature, but hell, why not?

+1
source

Source: https://habr.com/ru/post/1441365/


All Articles