减法 (–)Subtraction (-)

减法运算符 (-) 从第一个操作数中减去第二个操作数。The subtraction operator (-) subtracts the second operand from the first. 两个操作数可同时为整型或浮点型,或者一个操作数为指针,另一个操作数为整数。Both operands can be either integral or floating types, or one operand can be a pointer and the other an integer.

在将两个指针相减时,会通过将差值除以指针寻址的类型值的大小来将该差值转换为带符号的整数值。When two pointers are subtracted, the difference is converted to a signed integral value by dividing the difference by the size of a value of the type that the pointers address. 整数值的大小由标准包含文件 STDDEF.H 中的类型 ptrdiff_t 定义。The size of the integral value is defined by the type ptrdiff_t in the standard include file STDDEF.H. 结果表示两个地址间的类型的内存位置数。The result represents the number of memory positions of that type between the two addresses. 只能保证结果对于同一数组的两个元素有意义,如指针算法中讨论的那样。The result is only guaranteed to be meaningful for two elements of the same array, as discussed in Pointer Arithmetic.

在用指针值减去整数值时,减法运算符会通过将整数值乘以指针寻址的值的大小来转换整数值 (i)。When an integer value is subtracted from a pointer value, the subtraction operator converts the integer value (i) by multiplying it by the size of the value that the pointer addresses. 转换后,整数值表示 i 个内存位置,其中每个位置均具有指针类型所指定的长度。After conversion, the integer value represents i memory positions, where each position has the length specified by the pointer type. 当用指针值减去转换后的整数值时,结果将为原始地址前的内存地址 i 位置数。When the converted integer value is subtracted from the pointer value, the result is the memory address i positions before the original address. 新指针指向通过原始指针值寻址的类型的值。The new pointer points to a value of the type addressed by the original pointer value.

另请参阅See Also

C 加法运算符C Additive Operators