I have found in some code NULL pointer is defined as follows -
#define NULL ((char *)0)
I found these code compiles fine. But I did not understand how this works. Can anyone please explain how 0 is casted to a char pointer?
And is it valid to use it as a FILE pointer making null -
FILE *fp = NULL;
((void *)0)instead. As to how it works: the standard defines a conversion from an integer constant with the value 0 to a pointer, and specifies that the result will always be a null pointer.warning: "NULL" redefined [enabled by default] /usr/lib/gcc/i686-linux-gnu/4.6/include/stddef.h:402:0: note: this is the location of the previous definitionBut I did not understand how this works.void *was uniformly supported. When the C standard was published (1989. 1990), it became less appropriate; it should be a((void *)0)instead. Maybe your code is old enough that it is using the pre-standard definition.((char *)0)is not a valid null pointer constant (in C since the 1989 ANSI standard) and therefore not a valid definition forNULL. Given that definition,FILE *fp = NULL;is a constraint violation, requiring a compile-time diagnostic (and perhaps rejecting the program).