日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/1/23 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.

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語言標準所規定的。

原文:http://blog.csdn.net/a2806005024/article/details/36422987

總結

以上是生活随笔為你收集整理的c语言 地址+1,C语言中,为什么指针表达式的值+1.对应的地址值却+4?/为什么两个数组元素的地址相减之差不为地址之差?...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。