是否 *p++解除引用后增加?

发布于 2025-01-05 02:17:22 字数 62 浏览 1 评论 0原文

我不太确定这里的顺序是什么。是吗: 1)增加指针p的值后取消引用它 2) 在增加指针 p 的值之前先取消引用它

I'm not really sure what the order here is. Is it:
1) Dereference the value of pointer p after increasing it
2) Dereference the value of pointer p before increasing it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

水晶透心 2025-01-12 02:17:22

增量和取消引用之间没有顺序。但是,* 运算符适用于p++ 的结果,即递增之前p 的原始值。

There is no ordering between the increment and the dereference. However, the * operator applies to the result of p++, which is the original value of p prior to the increment.

断桥再见 2025-01-12 02:17:22

运算符表中,您可以看到后缀运算符++ 的位置高于 * 一元运算符。

因此,*p++ 增加 p(而不是 *p),并返回 p 的地址值包含在增量之前(因为它是后缀++)。

但顺序取决于实施。它可以从取消引用 p 开始,然后增加它,并且它可以存储 p 的旧值,增加它,然后取消引用旧值。

In the operators table, you can see that the suffix operator ++ have higher place than the * unary operator.

Hence, *p++ increase p (and not *p), and return the value of the address that p contained before the increment (since it's the suffix ++).

But the order is implementation-depend. It may begin by dereferencing p, and then increase it, and it may store the old value of p, increase it, and then dereference the old value.

情深缘浅 2025-01-12 02:17:22

尝试一下。该程序

#include <stdio.h>

int main(void) {
    int p[2];
    int *q = p;
    p[0] = 10;
    p[1] = 100;

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

    return 0;
}

打印

10
100

显示 ++ 适用于 p,而不是 *p,并且增量发生在取消引用之后。

编辑:(感谢 @EricLippert 说服我退出 K & R)

不仅可能存在事后关系,而且根据 K & R,可能存在事后关系。 R第203页,一定有:

后缀表达式后跟 ++ 或 -- 运算符是后缀表达式。表达式的表达式的值就是操作数的值。 记下值后,操作数将增加 (++) 或减少 (--) 1。

(强调我的)

当然,我不相信 K & R 在存在多线程的情况下讲述了有关 C 语义的任何内容(根据 Wikipedia pthreads 规范于 1995 年发布),但对于单线程程序 K & R说的很清楚了

Try it. The program

#include <stdio.h>

int main(void) {
    int p[2];
    int *q = p;
    p[0] = 10;
    p[1] = 100;

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

    return 0;
}

prints

10
100

showing that the ++ applies to p, not to *p, and that the increment happens after the dereference.

EDIT: (Thanks to @EricLippert for convincing me to pull out K & R)

Not only may there be a happens-after relationship, but according to K & R page 203, there must be:

A postfix expression followed by a ++ or -- operator is a postfix expression. The value of the expression of the expression is the value of the operand. After the value is noted, the operand is incremented (++) or decremented (--) by 1.

(emphasis mine)

Granted, I don't believe that K & R says anything about the semantics of C in the presence of multithreading (according to Wikipedia the pthreads specification was released in 1995), but for a single-threaded program K & R is pretty clear.

梦里人 2025-01-12 02:17:22

给定q = *p++;,q 获取 p 在增量之前指向的值。另一种说法是,表达式*p++的值是p在递增之前所指向的值。

Given q = *p++;, q gets the value that p pointed to before the increment. Another way to say it is that the value of the expression *p++ is the value that p pointed to before being incremented.

╰沐子 2025-01-12 02:17:22

后缀 ++-- 运算符本质上比前缀一元运算符具有更高的优先级。因此,*p++ 等价于*(p++);它递增p,并返回p在递增p之前所指向的值。

要增加 p 指向的值,请使用 (*p)++ (或者可能是 ++*p,如果计算顺序为副作用并不重要)。

The postfix ++ and -- operators essentially have higher precedence than the prefix unary operators. Therefore, *p++ is equivalent to *(p++); it increments p, and returns the value which p pointed to before p was incremented.

To increment the value pointed to by p, use (*p)++ (or perhaps ++*p, if the evaluation order of the side effect doesn't matter).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文