Skip to content

[dcl.array]/7 describes pointer arithmetic in a non-standard way and prematurely decays arrays #1645

@cubbimew

Description

@cubbimew

A note in [dcl.array]/7 says

"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."

and then in an Example inside that Note, describing int x[3][5];

"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."

I see multiple issues:

  1. "which itself is immediately converted into a pointer." is wrong: both explicit * and subscripting yield array lvalues, not pointers.
  2. "x+i is converted to the type of x" makes no sense: the type of x+i is int(*)[5] and it is not converted to anything, definitely not to the type of x, which is int[3][5]
  3. "multiplying i by the length of the object to which the pointer points" makes sense at assembly level, but is not how pointer arithmetic is defined in expr.add/4: x+i yields a pointer to the i'th element of x. In fact, the note just above this one gets it right with "E1[E2] refers to the E2-th member of E1"
  4. "which in turn is converted to a pointer to the first of the integers" is again prematurely decaying an array: x[i] or, equivalently, *(x+i) is an int[5] lvalue, not a pointer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions