c语⾔地址+1,C语⾔中,为什么指针表达式的值+1.对应的地址值却+4?为什么两个数组元素。。。
在C语⾔中,我们常常⽤到的⼀个运算是让某个变量的值+1.
例如 M = M + 1。
⽽在实际运⽤中,我们发现
对于指针进⾏+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.
sizeof 指针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标准当中,见下⽂粗体。
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 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语⾔标准所规定的。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论