Title
When is the array-to-pointer conversion applied?
Status
cd2
Section
9.3.4.5 [dcl.array]
Submitter
Eelis van der Weegen

Created on 2008-07-13.00:00:00 last changed 170 months ago

Messages

Date: 2010-03-15.00:00:00

[Voted into WP at March, 2010 meeting.]

Date: 2009-11-15.00:00:00

Proposed resolution (November, 2009):

Change paragraphs 6-8 of 9.3.4.5 [dcl.array] into a note and make the indicated changes:

[Note: Except where it has been declared for a class (12.4.5 [over.sub]), the subscript operator [] is interpreted in such a way that E1[E2] is identical to *((E1)+(E2)). Because of the conversion rules that apply to +, if E1 is an array and E2 an integer, then E1[E2] refers to the E2-th member of E1. Therefore, despite its asymmetric appearance, subscripting is a commutative operation.

A consistent rule is followed for multidimensional arrays. If E is an n-dimensional array of rank i × j × . . . × k, then E appearing in an expression that is subject to the array-to-pointer conversion (7.3.3 [conv.array]) is converted to a pointer to an (n-1)-dimensional array with rank j × . . . × k. If the * operator, either explicitly or implicitly as a result of subscripting, is applied to this pointer, the result is the pointed-to (n-1)-dimensional array, which itself is immediately converted into a pointer.

[Example: consider

    int x[3][5];

Here x is a 3 × 5 array of integers. When x appears in an expression, it is converted to a pointer to (the first of three) five-membered arrays of integers. In the expression x[i] which is equivalent to *(x+i), x is first converted to a pointer as described; then x+i is converted to the type of x, which involves multiplying i by the length of the object to which the pointer points, namely five integer objects. The results are added and indirection applied to yield an array (of five integers), which in turn is converted to a pointer to the first of the integers. If there is another subscript the same argument applies again; this time the result is an integer. —end example]end note]

Date: 2022-09-25.18:08:42

Paragraph 7 of 9.3.4.5 [dcl.array] says,

If E is an n-dimensional array of rank i × j × ... × k, then E appearing in an expression is converted to a pointer to an (n - 1)-dimensional array with rank j × ... × k.

This formulation does not allow for the existence of expressions in which the array-to-pointer conversion does not occur (as specified in Clause 7 [expr] paragraph 9). This paragraph should be no more than a note, if it appears at all, and the wording should be corrected.

History
Date User Action Args
2010-03-29 00:00:00adminsetmessages: + msg2705
2010-03-29 00:00:00adminsetmessages: + msg2704
2010-03-29 00:00:00adminsetstatus: tentatively ready -> cd2
2010-02-16 00:00:00adminsetstatus: drafting -> tentatively ready
2008-10-05 00:00:00adminsetstatus: open -> drafting
2008-07-13 00:00:00admincreate