c语言加减乘除代码 第一个为啥吧减了1

1501人阅读
C语言(3)
在C语言中,我们常常用到的一个运算是让某个变量的值+1.
例如 M = M + 1。
而在实际运用中,我们发现
对于指针进行+1运算,算出来的结果是+4。
图中我们定义的&变量M 和指针Matrix如下:
int M = 3;
int* Matrix = {1,2,3};
可以看到,对于M和 Matrix ,+1运算的效果是不同的。
这个差异是因为C语言的标准中规定了 加法与减法运算对于地址的操作和对于值的操作是不同的,如下文中粗体所示:
3.3.6 Additive operators
& & 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
& & * both operands are pointers to qualified or unqualified versions of com or
& & * the left operand is a pointer to an object type and the right operand has integral type. (Decrementing is equivalent to subtracting 1.)
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 &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语言标准所规定的。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:87615次
积分:1028
积分:1028
排名:千里之外
原创:22篇
(1)(3)(9)(9)(6)【C语言】这道题答案是什么?解释一下吧,谢了1_百度知道
【C语言】这道题答案是什么?解释一下吧,谢了1
/zhidao/wh%3D600%2C800/sign=6cff90d5cf7/2fdda3cc7cd98d3fb80e7aec90a1.jpg" target="_blank" title="点击查看大图" class="ikqb_img_alink"><img class="ikqb_img" src="http://c<a href="http://c.hiphotos.baidu:///zhidao/pic/item/2fdda3cc7cd98d3fb80e7aec90a1./zhidao/wh%3D450%2C600/sign=758aa1df70cf3bc7e855c5e8efdda3cc7cd98d3fb80e7aec90a1.jpg" esrc="http
提问者采纳
道题答案为32 进入第一个for循环的条件是i为5的整数倍,即输出32再跳出进行最外层的i+=1操作;紧接着先对i进行加1,若加1后的值能被8整除,然后对i加1,
提问者评价
其他类似问题
为您推荐:
c语言的相关知识
其他1条回答
脖子已扭断
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁C语言里++是右往左算,a++*a为什么不算++呢?、好奇怪.a=3时,结果是9、右结合性去了哪里?a++*a
当 a=3时,结果是9、那右结合性去了哪里?为什么算结果的是吧不加1?
在表达式中,前缀++、--或后缀++,--都属于“附加效应”(或副作用),这些附加效应何时实现,在ANSI C中并没有明确规定,只是规定,该表达式计算完成(时间点)后,附加效应也完成了,所以有附加效应的表达式的移植性是没有的.为了增强可移植性,建议在必要的地方加括号,比如(a++)* a,或 (++a)*a
为您推荐:
其他类似问题
扫描下载二维码

我要回帖

更多关于 c语言自增自减运算符 的文章

 

随机推荐