tatsu99さんの回答への補足です。
C11規格書から引っ張っておきます。まずは添字演算子の定義から。
6.5.2.1 Array subscripting
Semantics
2 A postfix expression followed by an expression in square brackets [] is a subscripted
designation of an element of an array object. The definition of the subscript operator []
is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that
apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the
initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th
element of E1 (counting from zero).
(仮訳)
[]のような角括弧の式のような後置演算子は配列オブジェクトの添字指定です。添字演算子[]はE1[E2]が(*((E1)+(E2)))と同一であるというように定義されます。二項演算子+における変換規則によって、もしE1が配列オブジェクトで(これは配列オブジェクトの先頭要素へのポインタと等価ですが(訳注: array to pointer conversionのこと))E2が整数ならば、E1[E2]はE1のE2番目の要素を示します。
一方で配列の宣言は次のように定義されています。ここで[]は演算子ではありません。
6.7.6 Declarators
Syntax
1
declarator:
pointeropt direct-declarator
direct-declarator:
identifier
( declarator )
direct-declarator [ type-qualifier-listopt assignment-expressionopt ]
direct-declarator [ static type-qualifier-listopt assignment-expression ]
direct-declarator [ type-qualifier-list static assignment-expression ]
direct-declarator [ type-qualifier-listopt * ]
direct-declarator ( parameter-type-list )
direct-declarator ( identifier-listopt )
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/01/18 02:21