Take the address of a one-past-the-end array element via subscript: legal by the C++ Standard or not?

后端 未结 13 1557
傲寒
傲寒 2020-11-22 06:34

I have seen it asserted several times now that the following code is not allowed by the C++ Standard:

int array[5];
int *array_begin = &array[0];
int *ar         


        
13条回答
  •  暖寄归人
    2020-11-22 06:57

    It is perfectly legal.

    The vector<> template class from the stl does exactly this when you call myVec.end(): it gets you a pointer (here as an iterator) which points one element past the end of the array.

提交回复
热议问题