是什么(int_1 + = * pointer ++ = int_2 ++)< int_3是什么意思?
我正在阅读这个较早的答案,其中包含一段我无法理解的C代码.基本上看起来像这样:
I was reading this earlier answer which had a piece of C code I couldn't understand. It essentially looks like this:
if((int_1 += *pointer++ = int_2++) < int_3) continue;
我可以将其分解为这样的内容-
I can break it down to something like this -
这是什么意思?我可以起床了:
What does this mean? I can get up to about this point:
if((int_1 = int_1+ *pointer++ (unsure about this part))<int_3) continue;
因此对于初学者来说,这确实是非常糟糕的C代码.就像可怕的C代码一样.就像,我已经用C编码很长时间了,不得不使用 operator优先级图表,因为我从未遇到过如此糟糕的情况.因此,没有理由编写这样的东西-当然不在生产代码中,并且希望不作为类的一部分,因为您永远都不需要知道操作员优先级的这个特殊问题(来源:我以谋生为生).我要说的是,您所引用的源代码是不应该以这种方式编写的错误的C代码.
So for starters, this is really, really bad C code. Like, horrible C code. Like, I've been coding in C for a long time and had to pull up an operator precedence chart because I've never encountered something like this terrible. So there's no reason to write something like this - certainly not in production code, and hopefully not as part of a class because you shouldn't never need to know this particular quirk of operator precedence (source: I teach CS for a living). I would go so far as to say that the source that you're referencing is Bad C Code That Should Never Be Written That Way.
但是话虽这么说,让我们分解一下!这里的核心表达是
But with that said, let's break it down! The core expression here is
(int_1 += *pointer++ = int_2++) < int_3
在该内部表达式中有两个赋值运算符,它们具有相同的优先级,并且从右到左具有相同的组.这意味着等同于
In that inner expression are two assignment operators, which have equal precedence and group from right-to-left. That means this is equivalent to
(int_1 += (*pointer++ = int_2++)) < int_3
这是
- 增加
int_2
并存储其旧值. - 将该值存储在
pointer
所指向的位置,然后将指针前进到下一个位置. - 将该值添加到
int_1
. - 然后查看该值是否小于
int_3
.
- Increment
int_2
and store its old value. - Store that value at the place pointed at by
pointer
, then advance pointer to the next location. - Add that value to
int_1
. - Then see whether that value is less than
int_3
.
没有理由做这样的事情.只需写
There's no reason to do something like this. Just write
int val = int_2;
*pointer = val;
int_1 += val;
int_2++;
pointer++;
if (int_1 < int_3) {
...
}
是的!不要写这样的代码.曾经. :-)
So yeah! Don't write code like this. Ever. :-)