了解包含 '++' 的表达式的求值 和'->' C语言中的运算符

发布于 2024-07-17 20:42:54 字数 1213 浏览 7 评论 0原文

考虑这个例子:

struct {
    int num;
} s, *ps;

s.num = 0;
ps = &s;
++ps->num;

printf("%d", s.num); /* Prints 1 */

它打印 1
所以我明白这是因为根据运算符优先级,->高于++,所以值ps->num (即 0)首先被获取,然后 ++ 运算符对其进行操作,因此它将其增加到 1。

struct {
    int num;
} s, *ps;

s.num = 0;
ps = &s;
ps++->num;

printf("%d", s.num); /* Prints 0 */

在这个例子中,我得到了 0,但我没有明白为什么; 第一个示例的解释对于本示例应该是相同的。 但似乎该表达式的计算如下:
首先,运算符++进行操作,并且它对ps进行操作,因此它将其递增到下一个struct。 只有在 -> 运行时,它才执行任何操作,因为它只是获取下一个 structnum 字段,并且不对其执行任何操作。
但这与运算符的优先级相矛盾,即 -> 的优先级高于 ++

有人可以解释这种行为吗?

编辑
阅读了两个引用 C++ 优先级表的答案后,这些优先级表表明前缀 ++/-- 运算符的优先级低于 -> ,我做了一些谷歌搜索,并想出了这个链接,其中指出该规则也适用于 C 本身。 它完全符合并充分解释了这种行为,但我必须补充一点,此链接中的表格与我自己的 K&R ANSI C 副本中的表格相矛盾。因此,如果您对哪个来源是正确的有任何建议,我想知道。

谢谢。

Consider this example:

struct {
    int num;
} s, *ps;

s.num = 0;
ps = &s;
++ps->num;

printf("%d", s.num); /* Prints 1 */

It prints 1.
So I understand that it is because according to operators precedence, -> is higher than ++, so the value ps->num (which is 0) is firstly fetched and then the ++ operator operates on it, so it increments it to 1.

struct {
    int num;
} s, *ps;

s.num = 0;
ps = &s;
ps++->num;

printf("%d", s.num); /* Prints 0 */

In this example I get 0 and I don't understand why; the explanation of the first example should be the same for this example. But it seems that this expression is evaluated as follows:
At first, the operator ++ operates, and it operates on ps, so it increments it to the next struct. Only then -> operates and it does nothing because it just fetches the num field of the next struct and does nothing with it.
But it contradicts the precedence of operators, which says that -> have higher precedence than ++.

Can someone explain this behavior?

Edit:
After reading two answers which refer to a C++ precedence tables which indicate that a prefix ++/-- operators have lower precedence than ->, I did some googling and came up with this link that states that this rule applies also to C itself. It fits exactly and fully explains this behavior, but I must add that the table in this link contradicts a table in my own copy of K&R ANSI C. So if you have suggestions as to which source is correct I would like to know.

Thanks.

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

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

发布评论

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

评论(8

苍景流年 2024-07-24 20:42:54

C 中后自增 (ps++) 和前自增 (++ps) 具有不同的关联性。前者从左到右关联,后者从右关联-向左。 检查页面(尽管这是针对C++的,因此优先级可能会产生误导)。

在最后一个示例中,您将指针更改为超过 &s 末尾的指针。 您尚未更改受指点者的值。 如果需要递增num,则需要将++绑定到num

详细说明:

 ps++->num;

(假设的)编译器看到此表达式后,可能会将 ps 对象推送到堆栈,后跟 ++ 运算符, -> 运算符,最后是对象 - num。 在评估时,编译器应该从哪里开始? 它查看可用的运算符,即 ++->。 它选择ps++还是ps? 这里的优先级规则:由于->的优先级高于++,所以需要->来处理num< /code> 作为一个操作数,ps 作为另一操作数。 因此,正如您所观察到的那样,表达式的值变为 ps->num 即 0。 评估后 ps 会发生什么? 还记得吗,堆栈上还剩下另一个运算符? 因此,编译器将其应用于 ps,它现在指向 &s 后面的一个元素。

脚注:

ANSI/ISO C 标准不使用运算符优先级语法。 相反,它使用所谓的全因子语法。 这通常涉及严格的语法定义,其中点缀着许多非终结符,例如“主表达式”和“移位表达式”等。 这很难理解,但对于语言设计者或编译器供应商来说更容易理解。 此外,这样的语法能够轻松地对优先级进行编码。 然而,任何全因子语法都与运算符优先级语法兼容,这就是大多数书籍(和网站)所做的(有时也会搞砸)。

The post-increment (ps++) and the pre-increment (++ps) have different associativity in C. The former associates left-to-right whereas the latter associates right-to-left. Check this page out (though this is for C++, so the precedences may be misleading).

In your last example you are changing the pointer to one past the end of &s. You have not changed the value of the pointee. If you need to increment num, you need to bind the ++ to num.

Detailed explanation:

 ps++->num;

A (hypothetical) compiler on seeing this expression, may push the ps object to the stack followed by the ++ operator, the -> operator and finally the object -- num. While evaluationg, where should the compiler start? It looks at the available operators i.e. ++ and ->. Does it choose ps++ or ps? Here precedence rules: since -> has a higher precedence than ++, it takes -> to process with num as one operand and ps as the other operand. So, the value of the expression becomes ps->num i.e. 0 as you rightly observe. What happens to ps after the evaluation? Remember, there is another operator left on the stack? So, the compiler applies this to ps and it now points to one element past &s.

Footnote:

The ANSI/ISO C standard does not use a operator precedence grammar. Rather it uses what is known as a fully-factored grammar. This typically involves an exacting grammar definition dotted with a number of non-terminals like "primary-expression" and "shift-expression" and so on. This is hard to understand, but is easier for the language designer or the compiler vendor. Also, such a grammar is able to encode precedences easily. However, any fully-factored grammar is compatible with an operator-precedence grammar and this is what most books (and websites) do (and also mess up at times).

划一舟意中人 2024-07-24 20:42:54

即使 ++ 具有更高的优先级,这也不会改变值 -> 继续运行,因为它是后增量。 请参阅此代码,其中还有此行为的另一个示例:

int b = 5;
int a = b++ * 3;
b = 5;
int c = (b++) * 3;

printf("%i, %i, %i\n", a, b, c); // Prints 15, 6, 15

struct {
  int num;
} s, *ps;

s.num = 35;
ps = &s;
printf("%p\n", ps); // Prints the pointer
printf("%i\n", ps++->num); // Prints 35
printf("%p\n", ps); // Prints the increased pointer

printf("%d\n", s.num); /* Prints 35 */

Even if ++ had a higher priority, this wouldn't change the value -> operates on, as it's a post-increment. See this code that also has another example of this behaviour:

int b = 5;
int a = b++ * 3;
b = 5;
int c = (b++) * 3;

printf("%i, %i, %i\n", a, b, c); // Prints 15, 6, 15

struct {
  int num;
} s, *ps;

s.num = 35;
ps = &s;
printf("%p\n", ps); // Prints the pointer
printf("%i\n", ps++->num); // Prints 35
printf("%p\n", ps); // Prints the increased pointer

printf("%d\n", s.num); /* Prints 35 */
七秒鱼° 2024-07-24 20:42:54

b = ++a; 等价于:

a += 1;
b = a;

b = a++; 相当于:

b = a;
a += 1;

所以很清楚为什么你没有得到你想要的结果。 您所描述的内容相当于 (++ps)->num。

b = ++a; is equivalent to:

a += 1;
b = a;

b = a++; is equivalent to:

b = a;
a += 1;

So it's pretty clear why you don't get the result you look for. The thing you described would be equivalent to (++ps)->num.

阿楠 2024-07-24 20:42:54

“优先级”基本上是一个派生属性; 它遵循解析规则。
++ps->num 被解析为 ++(ps->num) /* 添加 () 是为了澄清解析规则 */
而 ps++->num 只能解析为 (ps++)->num。

"precedence" is basically a derived property; it follows from parsing rules.
++ps->num is parsed as ++(ps->num) /* () added for clarification of parsing rules */
whereas ps++->num can only be parsed as (ps++)->num.

心碎无痕… 2024-07-24 20:42:54

ps++->num 将指针 ps 加 1,然后读取其中的数据。 由于 ps 就在堆栈上的 s 之后,我相信指针很可能会指向自身,尽管我不确定这一点,而且并不重要。 基本上初始程序正在执行 ++(ps->num) 但没有括号。 要实现相同的效果,但在访问数据后,您必须执行 (ps->num)++,或者不使用括号:ps->num++。

由于 ps 只是一个指针,即使您更改了它的值,s 仍然保持不变。

ps++->num increments the pointer ps by 1, and then reads the data inside it. Since ps is just after s on the stack, I believe the pointer will most likely be pointing to itself, although this I'm not sure of, and is not important. basically The initial program was doing ++(ps->num) but without the brackets. To achieve the same thing but after you access the data, you would have to do (ps->num)++, or without the brackets: ps->num++.

And since ps is just a pointer, even though you changed its value, s still stays the same.

歌入人心 2024-07-24 20:42:54

我想这是因为它们具有不同的优先级,并且在同一组内,它们具有特定的关联性(例如求值顺序)

检查 这里。 后缀运算符与指针解析具有相同的优先级,但前缀运算符的优先级较低。

I guess that is because they have different priority, and within the same group, they have a specific associativity (eg. evaluation order)

Check here. The postfix operator has the same priority as the pointer resolution, but the prefix operator has a lower priority.

写给空气的情书 2024-07-24 20:42:54

这里可以看到 ++ 作为前缀的优先级低于 ->; ,但作为后缀,它具有与 -> 相同的优先级。 并且是从左到右求值的,所以先ps++,然后->。

编辑:这是针对 C++ 的,而不是 C。所以我的答案不正确。

Here you can see that ++ as a prefix has lower priority than ->, but as a postfix it has the same priority as -> and it is evaluated from left to right, so ps++ is done first and then ->.

Edit: this is for C++, not C. So my answer isn't correct.

风吹短裙飘 2024-07-24 20:42:54

优先级用于解决不明确的解析。 ++ps->num 可以解析为 ((++ps)->num)(++(ps->num) );; ++()-> 的相对优先级决定了后者是正确的解析。

对于 ps++->num,只有一种有效的解析:((ps++)->num),因此运算符的优先级并不重要。

Precedence is used to resolve ambiguous parsing. ++ps->num could be parsed as ((++ps)->num) or (++(ps->num)); the relative precedence of ++() and -> determines that the latter is the correct parsing.

For ps++->num, there is only one valid parsing: ((ps++)->num), so the precedence of the operators doesn't matter.

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