指针算术Pointer Arithmetic

仅当指针操作数寻址数组成员且整数值在同一数组的边界中产生偏移时,涉及指针和整数的加法运算才会提供有意义的结果。Additive operations involving a pointer and an integer give meaningful results only if the pointer operand addresses an array member and the integer value produces an offset within the bounds of the same array. 当整数值转换为地址偏移量时,编译器将假定只有大小相同的内存位置位于原始地址和该地址加上偏移量之间。When the integer value is converted to an address offset, the compiler assumes that only memory positions of the same size lie between the original address and the address plus the offset.

此假设对数组成员有效。This assumption is valid for array members. 按照定义,数组是一系列相同类型的值;数组元素位于连续内存位置。By definition, an array is a series of values of the same type; its elements reside in contiguous memory locations. 但是,任何类型(数组元素除外)的存储不一定由相同类型的标识符填充。However, storage for any types except array elements is not guaranteed to be filled by the same type of identifiers. 即,在内存位置(即使是相同类型的位置)之间可能会出现空白。That is, blanks can appear between memory positions, even positions of the same type. 因此,对任何值(数组元素除外)的地址进行加法或减法的结果是不确定的。Therefore, the results of adding to or subtracting from the addresses of any values but array elements are undefined.

同样,当两个指针值相减时,转换将假定只有相同类型的值(没有空白)位于操作数给定的地址之间。Similarly, when two pointer values are subtracted, the conversion assumes that only values of the same type, with no blanks, lie between the addresses given by the operands.

另请参阅See Also

C 加法运算符C Additive Operators