我可以像这样进行指针运算以避免让指针离开数组边界吗?

Can I do pointerarithmetic like this to avoid letting the pointer leave array bounds?

想象一下这样的代码:

void some_scope()
{
    int OriginPointer[SOME_GIVEN_AMOUNT];
    int *ActingPointer;

    ActingPointer = OriginPointer;

    while ((ActingPointer - OriginPointer) < SOME_GIVEN_AMOUNT)
    {
        *ActingPointer = SOME_ASSIGNMENT;
        ActingPointer++;
    }

    do
    {
        *ActingPointer = 0;
    } while ((ActingPointer != OriginPointer) && (ActingPointer-- != OriginPointer))
}

关于逻辑分支按照标准确保从左到右进行评估这一事实,并且关于条件&,一旦一个条件为假,它就会中断,是否有任何规则可以使这个片段无效?我知道有更有效甚至更好的追溯方式,但我特别感兴趣: do/while 循环的中断条件是否阻止通过关于使用指针算术的规则调用未定义的行为,导致指针离开边界或超过数组 1?

此循环后:

while ((ActingPointer - OriginPointer) < SOME_GIVEN_AMOUNT)
{
    *ActingPointer = SOME_ASSIGNMENT;
    ActingPointer++;
}

ActingPointer 最终等于 OriginPointer + SOME_GIVEN_AMOUNT,这作为一个指针是可以的(因为它指向末尾后面的一个元素),但是在 [= 的第一次迭代中写入它=13=] 循环是未定义的行为。

如果 ActingPointerdo 循环之前递减,则一切正常。

does the interupt condition of the do/while loop prevent from invoking undefined behavior through the rule about using pointer arithmetic that results in a pointer leaving the bounds or more as 1 past the array?

是的,因为它从数组后面的 1 处开始,并且不会在数组开头以下递减。