Why does the ternary operator behave in this _weirdly_ syntax?

I read the answer from Torvald on the linux mailing list ( here ) about a macro that can be used to determine if an expression is good. Here is the macro:

#define ICE_P(x) (sizeof(int) == sizeof(*(1 ? ((void*)((x) * 0l)) : (int*)1)))

So, Torvald explains why he does what he should do, and at what point he writes:

the rule is that if one of the sides of a triple operation with pointers is NULL, the end result is another type

I was puzzled by this statement, so I tested the behavior with the following code:

printf("%ld\n", sizeof(*(1 ? NULL : (int *)1)));
printf("%ld\n", sizeof(*(1 ? NULL : (char *)1)));

To my surprise, this (when compiling with gcc or clang) produces the following result:

4
1

, , , , gcc NULL int * char *, NULL , .

, ?

+4

Source: https://habr.com/ru/post/1695551/


All Articles