C语言中,为什么指针表达式的值+1.对应的地址值却+4?/为什么两个数组元素的地址相减之差不为地址之差?


在C语言中,我们常常用到的一个运算是让某个变量的值+1.

例如 M = M + 1。

而在实际运用中,我们发现

对于指针进行+1运算,算出来的结果是+4。

如下图

C语言中,为什么指针表达式的值+1.对应的地址值却+4?/为什么两个数组元素的地址相减之差不为地址之差?

图中我们定义的 变量M 和指针Matrix如下:

int M = 3;

int* Matrix = {1,2,3};


可以看到,对于M和 Matrix ,+1运算的效果是不同的。


这个差异是因为C语言的标准中规定了 加法与减法运算对于地址的操作和对于值的操作是不同的,如下文中粗体所示:


C89


3.3.6 Additive operators
Syntax


    additive-expression:
        multiplicative-expression
        additive-expression + multiplicative-expression
        additive-expression - multiplicative-expression


Constraints


For addition, either both operands shall have arithmetic type, or one operand shall be a pointer to an object type and the other shall have integral type. (Incrementing is equivalent to adding 1.)


For subtraction, one of the following shall hold:


    * both operands have arithmetic type;
    * both operands are pointers to qualified or unqualified versions of compatible object types; or
    * the left operand is a pointer to an object type and the right operand has integral type. (Decrementing is equivalent to subtracting 1.)


Semantics


If both operands have arithmetic type, the usual arithmetic conversions are performed on them.


The result of the binary + operator is the sum of the operands.


The result of the binary - operator is the difference resulting from the subtraction of the second operand from the first.


When an expression that has integral type is added to or subtracted from a pointer, the integral value is first multiplied by the size of the object pointed to. The result has the type of the pointer operand.
If the pointer operand points to a member of an array object, and the array object is large enough, the result points to a member of the same array object, appropriately offset from the original member. Thus if P points to a member of an array object, the expression P+1 points to the next member of the array object. Unless both the pointer operand and the result point to a member of the same array object, or one past the last member of the array object, the behavior is undefined. Unless both the pointer operand and the result point to a member of the same array object, or the pointer operand points one past the last member of an array object and the result points to a member of the same array object, the behavior is undefined if the result is used as the operand of a unary * operator.


当一个加法运算,加号左边的操作数是一个指针,而右边的操作数是一个整数时,这个整数值先乘以指针类型的大小(sizeof(int)),然后再加到左边的数上。


这就解答了标题所述的第一个问题。


而标准的描述中另外一个值得注意的点是,两个地址相减的值会是什么?

问题呈现如下:

C语言中,为什么指针表达式的值+1.对应的地址值却+4?/为什么两个数组元素的地址相减之差不为地址之差?

同样答案在C标准当中,见下文粗体。


When two pointers to members of the same array object are subtracted, the difference is divided by the size of a member. The result represents the difference of the subscripts of the two array members. The size of the result is implementation-defined, and its type (a signed integral type) is ptrdiff_t defined in the <stddef.h> header. As with any other arithmetic overflow, if the result does not fit in the space provided, the behavior is undefined. If two pointers that do not point to members of the same array object are subtracted, the behavior is undefined. However, if P points either to a member of an array object or one past the last member of an array object, and Q points to the last member of the same array object, the expression (Q+1) - P has the same value as (Q-P) + 1, even though Q+1 does not point to a member of the array object.


当同一个数组的两个成员的指针相减时,其差值为:地址值的差,再除以一个数组成员的size。这个结果代表了两个指针对应元素的下标之差。


所以大家才遇到了上图中所遇到的问题。这是C语言标准所规定的。

C语言中,为什么指针表达式的值+1.对应的地址值却+4?/为什么两个数组元素的地址相减之差不为地址之差?,布布扣,bubuko.com

C语言中,为什么指针表达式的值+1.对应的地址值却+4?/为什么两个数组元素的地址相减之差不为地址之差?

上一篇:Android学习(五)


下一篇:c++中引用的一些研究