c 中指针的运算符优先级
下面的情况如何分析优先级。
for (i=0; i<20; i++)
{
*array_p++ = i*i;
printf("%d\n",*arr++);
}
以下代码与上面有何不同。
for (int i=0; i<20; i++)
{
*arr = i*i;
printf("%d\n",*arr);
arr++;
printf("%d\n",(int )arr);
}
我期望相同的输出,但 *arr 值的输出不同
How to analyse the precedence in following situation .
for (i=0; i<20; i++)
{
*array_p++ = i*i;
printf("%d\n",*arr++);
}
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);
}
I am expecting same output but outputs are different for *arr value
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
后缀运算符的优先级高于一元运算符,因此
*x++
被解析为*(x++)
;表达式x++
(即x
)的结果被取消引用。在
*++x
的情况下,*
和++
都是一元运算符,因此具有相同的优先级,因此应用运算符从左到右,或*(++x)
;表达式++x
(即x + sizeof *x
)的结果被取消引用。Postfix operators have higher precedence than unary operators, so
*x++
is parsed as*(x++)
; the result of the expressionx++
(which isx
) is dereferenced.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 isx + sizeof *x
) is dereferenced.引用 Wikipedia,postfix ++ 绑定在一元 * 之前。这意味着您有
*(arr++)
。例如,在表达式*arr++ = 5
中,*arr
被赋值为 5,然后arr
递增。在K&R中,这个技巧被用来编写memcpy的简洁版本。就像:
今晚我回家后我会发布正确的例子。
编辑:显然,只有 postfix ++ 具有更高的优先级。维基百科说前缀 ++ 具有同等优先级。
Citing Wikipedia, postfix ++ binds before unary *. This means that you have
*(arr++)
. For example, in the expression*arr++ = 5
,*arr
is assigned to 5, thenarr
is incremented.In the K&R, this trick is used to write a concise version of memcpy. It's something like:
I'll post the correct example after I get home tonight.
Edit: Apparently, only postfix ++ has higher precedence. Wikipedia says prefix ++ has equal precedence.
当您考虑前缀增量时,很容易记住哪个优先。
这里什么优先?
*++数组=x; // 很明显,
后缀表达式具有相同的优先级规则,因此它相当简单。
对于强制转换运算符也是如此。
(somestruct *)x + 1;
首先完成转换,因为如果不是以下内容,则没有意义
x + (int)1;
It is easy to remember which has precedence when you consider prefix increment.
what has precedence here?
*++array = x; // pretty obvious
the same precedence rules go for the postfix expression so its rather easy.
The same goes for the cast operator.
(somestruct *)x + 1;
first the cast is done since if it wasn't the following wouldnt make sense
x + (int)1;
在第一个示例代码片段中,
array_ptr 首先递增,即地址,然后将根据 i*i 计算的值分配给 *array_ptr,因为计算按照从右到左的顺序,即 *(array_ptr ++)。
在代码片段的第二个示例中,
首先计算从 i*i 计算出的值并将其分配给 *arr,然后递增指向 arr 的指针。
因此,两个代码片段之间的值存在差异。
In the first sample code snippet,
array_ptr is incremented first i.e address, and then the value computed from i*i is assigned to *array_ptr since evaluation takes in the order of right to left i.e *(array_ptr ++).
In the second sample of code snippet,
value computed from i*i is first computed and assigned to *arr, then pointer pointing to arr is incremented.
Hence there is difference in values among the 2 code snippets.
运算符 ++ 和 * 具有相同的优先级(
后缀或前缀
)。但是,这种情况下的结合性是从右到左。因此,在类似的情况下,
此链接应该为您提供有关运算符优先级及其关联性的更多信息。
http://www.isthe.com/chongo/tech/comp /c/c-precedence.html
Operators ++ and * have the same precedence (
postfix or prefix
).However the associativity in such cases is from right to left.so in cases like
this link should give you more information on operators precedence and their associativity..
http://www.isthe.com/chongo/tech/comp/c/c-precedence.html