GA************@shepjeng.twbbs.org (I wish) wrote:
http://www.eskimo.com/~scs/C-faq/q5.4.html
It says "In particular, a cast may still be necessary before NULL
(as before 0) in a function call argument."
Does that mean if I want to pass a NULL to a function,
I should write like this
foo( (char *)NULL ); /* Assume foo receive a pointer to char */
Well, that's debatable.
If you have a decent prototype[1] in scope, and that prototype tells you
that the function is not for a variadic function (or that the pointer is
one of the fixed arguments to a variadic function, not part of the
variable arguments), then no, you don't need to.
If you don't have a prototype in scope, you need to, because then the
compiler can't figure out what type of pointer to convert the null
pointer constant to - it doesn't know what type is required unless the
prototype tells it.
If you the null pointer is one of the variable arguments to a variadic
function, you also need to cast, because the compiler can't figure out
what type of pointer is needed - it can in the fixed arguments, of
course, but there's surprisingly little type information in "...".
The above is all clear and not really debatable. The debate starts when
you ask whether, even though you usually don't _need_ to cast a null
pointer argument, you _should_ do so for stylistic reasons.
On one side of the argument, if you simply cast all null pointer
arguments, you don't need to think about it, and you won't forget to
cast one you must cast.
On the other side, you need to know about the difference between
variadic functions and normal ones anyway, and you shouldn't (and in
C99, can't) call functions without a proper declaration in the first
place; and any superfluous cast is a hinder to the legibility of your
code.
Me, I favour the second side, and only use properly prototyped functions
whenever I can help it, and cast only null pointer constants passed as
variadic arguments. But I don't pretend this is an immutable rule -
unless you're me.
Richard
[1] Or complete old-style definition, I presume; but just don't use
old-style definitions, it's safer. An old-style _declaration_ is not
good enough, in any case. (And the reason I say "I presume" is that
I'm not sure; and don't care to find out, because old-style
definitions are a Bad Thing anyway.)