From The Linux Programming Interface
execl(prog, arg, (char *) 0); execl(prog, arg, (char *) NULL);
Casting
NULL
in the manner of the last call above is generally required, even on implementations whereNULL
is defined as(void *) 0
.This is because, although the C standards require that null pointers of different types should test true for comparisons on equality, they don’t require that pointers of different types have the same internal representation (although on most implementations they do).
And, in a variadic function, the compiler can’t cast(void *) 0
to a null pointer of the appropriate type.The C standards make one exception to the rule that pointers of different types need not have the same representation: pointers of the types
char *
andvoid *
are required to have the same internal representation. This means that passing(void *) 0
instead of(char *) 0
would not be a problem in the example case ofexecl()
, but, in the general case, a cast is needed.
"Casting
NULL
in the manner of the last call above is generally required"Does the C standard requires the null pointer be represented the same as
(char*) 0
?"in a variadic function such as
execl()
, the compiler can’t cast(void *) 0
to a null pointer of the appropriate type."Is
(void *) 0
not a null pointer of a type?If yes, why can't the compiler cast
(void *) 0
inexecl(prog, arg, (void*) 0)
to "a null pointer of the appropriate type"?"pointers of the types
char *
andvoid *
are required to have the same internal representation. This means that passing(void *) 0
instead of(char *) 0
would not be a problem in the example case ofexecl()
".Can the compiler cast
(void *) 0
inexecl(prog, arg, (void*) 0)
to "a null pointer of the appropriate type" now?Why does it contradict to the quote in my point 2?
If I replace
(void *) 0
inexecl(prog, arg, (void*) 0)
with cast of 0 to any type's pointer, such as(int *) 0
, can the compiler cast(int *) 0
inexecl(prog, arg, (int*) 0)
to "a null pointer of the appropriate type"? Thanks.For a non-variadic function call, such as in
sigaction(SIGINT, &sa, (int*) 0)
, can the compiler cast(int *) 0
to "a null pointer of the appropriate type"?
Thanks.