2
votes

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?

Is pointer treated as int in nature, or the ! do a type convert?


I found the c99 reference mentioned in answer here: www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf

2

2 Answers

4
votes

From c99 standard, chapter 6.5.3.3, paragraph 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,

Arithmetic types and pointer types are collectively called scalar types.

So, one can use the pointer type directly with the unary ! 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.

1
votes

Whatever, the pointer is just an address of something, i.e. an Number. So '!' operator would play as usual with the pointer too.