为什么* p ++首先取消引用指针,然后再增加指针地址?

问题描述 投票:1回答:2

C运算符优先级图表如下所示:C operator precedence

为什么列表中的文章首先增加/减少,但是* p ++导致首先取消引用指针,然后再增加指向的指针地址?

c operators operator-precedence
2个回答
3
投票

[*p++解析为*(p++)

p++的值为p(并且副作用正在增加p),所以*p++的值与*p相同。

除了副作用,*p*p++相同。

与整数完全相同的事情:

int n = 6;
printf("%d\n", 7 * n);
printf("%d\n", 7 * n++); // except for the side-effect same value as above

-3
投票

在p ++中,因为++出现在p的[[after中,所以它表示后递增。这意味着,如果要使用p执行某些任务,则将使用值p,并且仅在完成该任务后才递增p。以下示例:

#include<stdio.h> int main() { int a, b; a = 19; b = 10; if (a++ == 19) // 19 == 19. Checks out. // ------------------- // now that the line in which a++ appreared has // finished executing, the post increment comes into play // and a becomes 20 printf("%d\n", a); // 20 is printed if (++b == 10) // 11 != 10. Code inside curly braces of if statement // is not executed printf("%d\n", b); // nothing is printed // final output at the console // 20 is printed return 0; }
因此,在您的情况下,首先将指针p原样使用并取消引用。只有在该行完成执行后,++才会起作用,并增加p中存储的地址。
© www.soinside.com 2019 - 2024. All rights reserved.