Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cubbimew opened this issue Jun 16, 2017 · 0 comments

Comments

@cubbimew
Copy link
Contributor

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant