这是未定义的行为吗?
arr[i++] += x; // [1]
// arr is an array of integer type and x is an integer type variable
// Assumptions -
// 1. Value of i is a valid index value of array arr and
// the result of prefix/postfix increment/decrement of i
// is also a valid index value of array arr.
// 2. The value of array members and variable x is such that
// arithmetic operation (+) does not result in overflow.
gcc
并且clang
编译器不会在语句上给出任何警告消息[1]
,但如果我[1]
用此语句替换 -
arr[i++] = arr[i++] + x; // [2]
他们对这个声明发出了一个警告信息,这是非常明显的——
gcc
编译器:
warning: operation on ‘i’ may be undefined [-Wsequence-point]
arr[i++] = arr[i++] + x;
clang
编译器:
warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
arr[i++] = arr[i++] + x;