4

I was reading an answer from Torvald in the linux mailing (here) about a macro that could be used to determine if an expression is an ICE. Here is the macro:

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

So Torvald explains why it's doing what it's supposed to do and at what point he writes:

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

I was perplex about that statement so I tested the behaviour 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 compiled with either gcc or clang) prints the following output:

4
1

Now after a bit of thinking I believe what's happening here is that gcc is implicitly casting NULL to either an int * or char * and then returning that NULL with those types, thus giving the previous answer.

Is that the case or am I misunderstanding what's going on?

Alex Garcia
  • 773
  • 7
  • 21

0 Answers0