Treating a single object like an array with one element, taking one-past-end pointer

前端 未结 1 1570
旧巷少年郎
旧巷少年郎 2020-12-11 19:02

Preamble: It is well-known that taking the pointer one past the end of an array is legal and well-defined:

int main()
{
  int na [1] = {};
         


        
相关标签:
1条回答
  • 2020-12-11 19:26

    The answer is in the paragraph before the one you quote:

    4/ For the purposes of these operators, a pointer to a nonarray object behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type.

    (Note: I'm quoting C++11 as I don't have C++03 to hand. I'm fairly sure nothing has changed.)

    So yes, &na + 1 is a valid past-the-end pointer.

    0 讨论(0)
提交回复
热议问题