I gone through this question -
why the result of : 1 ? (int *)0 : (void *)0
differs to the result of :
1 ? (int *)0 : (void *)1
How it is differ ? It should be 0
or (int*)0
.
How to check the result ?
Where we can use such type of expression ?
So effeffe already answered on why the two expressions differ:
To answer this question now:
With
gcc
you can usetypeof
extension and__builtin_types_compatible_p
builtin function:The only difference is in types: the first one returns an
int *
, the second one returns avoid *
.From C11 standard, §6.5.15 Conditional operator, ¶6:
(emphasis mine)
Remember that a pointer to non-
void
cannot be a null pointer constant but just a null pointer. C11 §6.3.2.3 Pointers, ¶3:So, here:
(int *) 0
is just a null pointer while(void *) 0
is a null pointer constant, so the result has typeint *
("if one operand is a null pointer constant, the result has the type of the other operand").while here:
there are no null pointer constants (only a null pointer, the first one), so the result has the composite type
void *
("if both operands are pointers to compatible types or to differently qualified versions of compatible types, the result type is a pointer to an appropriately qualified version of the composite type").The results have different types but they are both null pointers. Also note that the result is never
0
as you say in your question, it's always a pointer.Unfortunately there is no standard way to see the difference in C, C++ has some support about it (
typeinfo
) but results are different there.I can't think about a useful and concrete use of this obscure corner of the language.