在C的指针操作符precedence [英] operator precedence in c with pointers
问题描述
如何分析precedence在以下情况。
How to analyse the precedence in following situation .
for (i=0; i<20; i++)
{
*array_p++ = i*i;
printf("%d\n",*arr++);
}
如何以下code与上述不同。
how is following code different from above.
for (int i=0; i<20; i++)
{
*arr = i*i;
printf("%d\n",*arr);
arr++;
printf("%d\n",(int )arr);
}
我期待相同的输出,但输出为* ARR值不同。
I am expecting same output but outputs are different for *arr value
推荐答案
Postfix的运营商有更高的precedence比一元运算符,所以 * X ++
被解析为 *(X ++)
;恩pression 的结果X ++
(这是 X
)将被取消引用。
Postfix operators have higher precedence than unary operators, so *x++
is parsed as *(x++)
; the result of the expression x++
(which is x
) is dereferenced.
在 * + X
,无论 *
和 ++ <的情况下, / code>是一元运算符,因而具有相同的precedence,所以运营商应用左到右,或
*(++ x)
;恩pression 的结果++ X
(这是 X + sizeof的* X
)将被取消引用。
In the case of *++x
, both *
and ++
are unary operators and thus have the same precedence, so the operators are applied left-to-right, or *(++x)
; the result of the expression ++x
(which is x + sizeof *x
) is dereferenced.
这篇关于在C的指针操作符precedence的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!