[英]What is &&& operation in C
#include <stdio.h>
volatile int i;
int main()
{
int c;
for (i = 0; i < 3; i++)
{
c = i &&& i;
printf("%d\n", c);
}
return 0;
}
The output of the above program compiled using gcc
is 使用
gcc
编译的上述程序的输出是
0
1
1
With the -Wall
or -Waddress
option, gcc
issues a warning: 使用
-Wall
或-Waddress
选项, gcc
发出警告:
warning: the address of ‘i’ will always evaluate as ‘true’ [-Waddress]
How is c
being evaluated in the above program? 如何在上述计划中评估
c
?
It's c = i && (&i);
这是
c = i && (&i);
, with the second part being redundant, since &i
will never evaluate to false
. ,第二部分是多余的,因为
&i
永远不会评估为false
。
For a user-defined type, where you can actually overload unary operator &
, it might be different, but it's still a very bad idea . 对于用户定义的类型,你可以实际上重载一元运算
operator &
,它可能是不同的,但它仍然是一个非常糟糕的主意 。
If you turn on warnings , you'll get something like: 如果你打开警告 ,你会得到类似的东西:
warning: the address of 'i' will always evaluate as 'true'
警告:'i'的地址将始终评估为'true'
There is no &&&
operator or token in C. But the &&
(logical "and") and &
(unary address-of or bitwise "and") operators do exist. C中没有
&&&
运算符或标记。但是&&
(逻辑“和”)和&
(一元地址或按位“和”)运算符确实存在。
By the maximal munch rule, this: 通过最大的蒙克规则,这个:
c = i &&& i;
is equivalent to this: 相当于:
c = i && & i;
It sets c
to 1 if both i
and &i
are true, and to 0 if either of them is false. 如果
i
和&i
都为真,则将c
设置为1,如果其中任何一个为假,则将其设置为0。
For an int, any non-zero value is true. 对于int,任何非零值都为true。 For a pointer, any non-null value is true (and the address of an object is always non-null).
对于指针,任何非null值都为true(并且对象的地址始终为非null)。 So:
所以:
It sets c
to 1 if i
is non-zero, or to 0
if i
is equal to zero. 它设置
c
为1,如果i
是非零,或者0
,如果i
是等于零。
Which implies that the &&&
is being used here just for deliberate obfuscation. 这意味着
&&&
在这里被用于故意混淆。 The assignment might as well be any of the following: 作业也可以是以下任何一种:
c = i && 1;
c = !!i;
c = (bool)i; // C++ or C with <stdbool.h>
c = i ? 1 : 0; /* C */
c = i ? true : false; // C++
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.