在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 h