C allows NULL to be defined to any null pointer constant, in other words, any integer constant expression that evaluates to 0, or such an expression cast to void *. My question concerns whether the choice of definition really matters, i.e. whether an otherwise-correct program might depend on which definition is used. For the purpose of this question, I'd like to ignore issues like NULL being passed to variadic functions or functions lacking prototypes, since I've already dealt with it separately. Let's assume sizeof NULL == sizeof(void *) and sizeof NULL == sizeof(T) for some integer type T, so that sizeof is not sufficient to answer the question of whether NULL has pointer type.
Obviously, C11 provides a way to distinguish the type of NULL or any other expression: the _Generic keyword.
C99 also provides one obscure way that seems to be reliable:
int null_has_ptr_type()
{
char s[1][1+(int)NULL];
int i = 0;
return sizeof s[i++], i;
}
Are there any other methods by which the type of NULL may be determined by a conforming C program? Any that work in C89?
char c = NULL;generates compiler warnings ifNULLis of typevoid *(so the cast is present).null_has_ptr_typereally work? It returns 0 in both case on my computer.return sizeof s[i++], i;will return i.sis a VLA,i++is evaluated, otherwise it's not. Andswill be a VLA if NULL is a pointer, not if it's an int. Or something like that... GCC gives different warnings in both cases. @Kirilenko