C指针的一元加法如何工作? [英] How does unary addition on C pointers work?

查看:89
本文介绍了C指针的一元加法如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一元运算符++将一个加到一个数字上.但是,我发现如果在int指针上执行此操作,它将增加4(系统上int的大小).为什么这样做呢?例如,以下代码:

I know that the unary operator ++ adds one to a number. However, I find that if I do it on an int pointer, it increments by 4 (the sizeof an int on my system). Why does it do this? For example, the following code:

int main(void)
{
  int *a = malloc(5 * sizeof(int));
  a[0] = 42;
  a[1] = 42;
  a[2] = 42;
  a[3] = 42;
  a[4] = 42;
  printf("%p\n", a);
  printf("%p\n", ++a);
  printf("%p\n", ++a);
  return 0;
}

将返回三个数字,每个数字之间相差4.

will return three numbers with a difference of 4 between each.

推荐答案

C就是这样-规范中有完整说明,第6.5.6节加法运算符,第8段:

It's just the way C is - the full explanation is in the spec, Section 6.5.6 Additive operators, paragraph 8:

将具有整数类型的表达式添加到指针或从指针中减去时,结果将具有指针操作数的类型.如果指针操作数指向数组对象的元素,并且数组足够大,则结果指向与原始元素偏移的元素,以使结果数组元素和原始数组元素的下标之差等于整数表达式.换句话说,如果表达式 P 指向数组对象的第 i 个元素,则表达式 (P)+N (等效, N+(P) )和 (P)-N (其中 N 的值为 n )指向,分别是数组对象的 i + n -th和 i - n -th个元素,只要它们存在.此外,如果表达式 P 指向数组对象的最后一个元素,则表达式 (P)+1 指向一个数组对象的最后一个元素,如果表达式 Q 指向数组对象的最后一个元素,表达式 (Q)-1 指向数组对象的最后一个元素.如果指针操作数和结果都指向同一数组对象的元素,或者指向数组对象的最后一个元素,则求值不会产生溢出;否则,行为是不确定的.如果结果指向数组对象的最后一个元素之后,则不应将其用作一元被评估的 * 运算符的操作数.

When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integer expression. In other words, if the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N (where N has the value n) point to, respectively, the i+n-th and in-th elements of the array object, provided they exist. Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object. If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated.

要将其与前缀++运算符的使用相关联,还需要阅读第6.5.3.1节前缀递增和递减运算符,第2段:

To relate that to your use of the prefix ++ operator, you need to also read Section 6.5.3.1 Prefix increment and decrement operators, paragraph 2:

前缀 ++ 运算符的操作数的值增加.结果是递增后的操作数的新值.表达式 ++E 等同于 (E+=1) .

The value of the operand of the prefix ++ operator is incremented. The result is the new value of the operand after incrementation. The expression ++E is equivalent to (E+=1).

还有第6.5.16.2节化合物分配,第3段:

格式为 E1 化合物赋值与简单赋值表达式不同 E1 = E1 op (E2) ,仅对左值 E1 进行一次评估.

A compound assignment of the form E1 op= E2 differs from the simple assignment expression E1 = E1 op (E2) only in that the lvalue E1 is evaluated only once.

这篇关于C指针的一元加法如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆