gpt4 book ai didi

c++ - printf 中的优先级似乎发生了变化

转载 作者:行者123 更新时间:2023-11-30 21:41:50 25 4
gpt4 key购买 nike

假设你有一个int指针变量int* q = malloc(sizeof(int))

现在正在做

*q++;

q将指向自 ++ 以来的某个其他内存位置具有优先权

但是在 printf 中做同样的事情

printf("%d",*q++);

赋予解引用(*) 运算符优先级 ??

最佳答案

++ 是后缀增量。它优先于一元 *。然而,更新变量的副作用是在变量求值之后完成的(6.5.2.4):

The value computation of the result is sequenced before the side effect of updating the stored value of the operand.

因此,*q++ 始终会为您提供 *q 的值,无论它在代码中的何处使用。

这是前缀增量和后缀增量之间的区别。 *++q 会先递增指针,然后计算新地址的值。

关于c++ - printf 中的优先级似乎发生了变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36352202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com