简体   繁体   English

逻辑非运算符

[英]Logical not operator on pointer

Code: 码:

struct person *p = NULL;
printf("%d, %d\n", !p, !!p);

In above code, the ! 在上面的代码中, ! operator works on pointer, I know ! 我知道运算符在指针上工作! works with int , but what happens when it works with pointer ? int ,但是与pointer一起使用会发生什么呢?

Is pointer treated as int in nature, or the ! 本质上将指针视为int! do a type convert? 类型转换吗?


I found the c99 reference mentioned in answer here: www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf 我在这里找到答案中提到的c99参考: www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf

From c99 standard, chapter 6.5.3.3, paragraph 1 根据c99标准的第6.5.3.3章第1段

The operand of the unary + or - operator shall have arithmetic type; 一元+或-运算符的操作数应具有算术类型; of the ~ operator, integer type; 〜运算符的整数类型; of the ! 的! operator, scalar type. 运算符,标量类型。

and , from 6.2.5, paragraph 21, 以及,从6.2.5第21段开始,

Arithmetic types and pointer types are collectively called scalar types. 算术类型和指针类型统称为标量类型。

So, one can use the pointer type directly with the unary ! 因此,可以直接将pointer类型与一元函数一起使用! operator. 操作员。 The ! ! is evaluated normally. 正常评估。

Maybe worthy to mention, in case of pointer usage, NULL value is a False any value other than NULL is considered True . 也许值得一提的是,在使用pointer情况下, NULL值是False,任何非NULL被视为True

Whatever, the pointer is just an address of something, ie an Number. 无论如何,指针只是某物的地址,即数字。 So '!' 所以'!' operator would play as usual with the pointer too. 操作符也将照常使用指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM