Does pointer arithmetic still work outside the array?

前端 未结 3 1856
孤街浪徒
孤街浪徒 2021-01-04 19:43

I am always reading that pointer arithmetic is defined as long as you don\'t leave the bounds of the array. I am not sure I completely understand what this means and I was a

3条回答
  •  遥遥无期
    2021-01-04 20:21

    According to the C11 standard, §6.5.6/8 (I put in the first part for context):

    When an expression that has integer type is added to or subtracted from a pointer
    ...
    If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.

    Therefore, a result that is outside of the array and not one past the end is undefined behaviour.

提交回复
热议问题