为什么将不输出在这种情况下,4? [英] Why won't the output be 4 in this case?

查看:120
本文介绍了为什么将不输出在这种情况下,4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
int main()
{
    int arr[] = {1, 2, 3, 4, 5};
    int *p = arr;
    ++*p;
    p += 2;
    printf("%d", *p);
    return 0;
}

的precedence preFIX ++ 大于非关联化,因此, P 现在应该指向第二元件。因此,当我们添加 2 来,它应该指向第四元素,答案应该是 4 。但是,答案是 3 ,这是为什么?

The precedence of prefix ++ is greater than dereferencing, and hence, p should now point to the second element. And therefore, when we add 2 to it, it should point to the 4th element and answer should be 4. But, the answer is 3, why is that?

推荐答案

在除权pression像 ++ * P precedence没有发挥任何角色在所有。在这前pression内运营商适用于 P * 在这种情况下)。外操作符( ++ )适用于内部运营的结果。

In the expression like ++*p precedence does not play any role at all. In this expression the inner operator applies to p (* in this case). The outer operator (++) applies to the result of the inner operator.

如果你交换他们周围,以获得 * ++ P ,那么 ++ 将适用于 p ,而 * 将适用于 ++ p 。

If you swap them around to obtain *++p, then ++ will apply to p, while * will apply to the result of ++p.

你有一堆一元/后缀运算符围坐在一起的每一次的操作数的同一侧的,它们适用于由内向外的顺序。

Every time you have a bunch of unary/postfix operators sitting together on the same side of the operand, they apply in the inside-out order.

对于右边的例子,在点+ [I] 运营商 ++ 适用于 p [I] 适用于点++ 的结果。同时,在 P [I] ++ 运营商 [I] 适用于 P ++ 适用于 p [I] 的结果

For a right-hand side example, in p++[i] operator ++ applies to p and [i] applies to the result of p++. Meanwhile, in p[i]++ operator [i] applies to p and ++ applies to the result of p[i].

precedence开始于暧昧的情况下发挥自己的作用,如:

Precedence begins to play its role in "ambiguous" cases like:


  • 一元/后缀操作符与二元运算符,例如

  • Unary/postfix operator vs. binary operator, e.g.

*p + 2

在上述情况下,一元 * 具有较高的precedence不是二进制 + ,致使(* p)+ 2

In the above case unary * has higher precedence than binary +, resulting in (*p) + 2.

p->i / 5

下面后​​缀 - &GT; 具有较高的precedence不是二进制 / ,致使(对GT; I)/ 5

Here postfix -> has higher precedence than binary /, resulting in (p->i) / 5.

和一般一元/后缀运营商有更高的precedence不是二进制运算符。

And in general unary/postfix operators have higher precedence than binary operators.

单目与后缀操作符,即运营商的两个的操作数的两侧,例如

Unary vs. postfix operator, i.e. operators on both sides of the operand, e.g.

*p++

在上述情况下,后缀 ++ 具有更大的precedence比一元 * ,致使 *(p ++)

In the above case postfix ++ has greater precedence than unary *, resulting in *(p++).

&p->i

下面后​​缀 - &GT; 具有更大的precedence比一元&安培; ,致使及(p-I标记)。

Here postfix -> has greater precedence than unary &, resulting in &(p->i).

和一般后缀运营商有更高的precedence比一元运算符。

And in general postfix operators have higher precedence than unary operators.

各种更多的异国情调的情况下,像运营商...

Various more "exotic" cases like ?: operator...

这篇关于为什么将不输出在这种情况下,4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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