[comp.lang.c] Explicit cast of NULL

henry@utzoo.uucp (Henry Spencer) (06/27/90)

In article <9473@hubcap.clemson.edu> grimlok@hubcap.clemson.edu (Mike Percy) writes:
>or c) simply use NULL everywhere, but make sure that a prototype is in
>      scope for every function call...

Not good enough when passing pointers to varargs functions.
-- 
"Either NFS must be scrapped or NFS    | Henry Spencer at U of Toronto Zoology
must be changed."      -John Osterhout |  henry@zoo.toronto.edu   utzoo!henry

karl@haddock.ima.isc.com (Karl Heuer) (06/28/90)

In article <9473@hubcap.clemson.edu> grimlok@hubcap.clemson.edu (Mike Percy) writes:
>As near as I can tell, the need for explicit casts on NULL pointers _IS_
>obsolete.  Don't continue to propogate obsolete-isms.  Use prototyping
>utilities to fix old programs.

The problem is with old *compilers*, not old *programs*.  ANSI compilers are
not yet ubiquitous, so the careful programmer must still use explicit casts
in function arguments to make sure that the program will work on pre-ANSI
implementations.  An ANSI-to-Classic conversion tool could do the job, but
I haven't heard of one that inserts the necessary casts.

Karl W. Z. Heuer (karl@kelp.ima.isc.com or ima!kelp!karl), The Walking Lint

martin@mwtech.UUCP (Martin Weitzel) (06/28/90)

In article <9473@hubcap.clemson.edu> grimlok@hubcap.clemson.edu (Mike Percy) writes:
>martin@mwtech.UUCP (Martin Weitzel) writes:
[about explicit casts of NULL-pointers]

>>IMHO, if ANSI-C together with compilers that implement it *and*
>>function prototypes will be in broad use (1..3 years from now, I
>>suppose), explicit casts for NULL-pointers should become obsolete.
> 
>As near as I can tell, the need for explicit casts on NULL pointers _IS_
>obsolete.

Read careful what I wrote! You assume a compiler which implements
prototypes. Many people have to support code that must be compiled
with different compilers. Even if four out of five *have* prototypes,
you must explicitly cast NULL-pointer function arguments for the
the one that doesn't have them!

>Don't continue to propogate obsolete-isms.  Use prototyping
>utilities to fix old programs.  I've been using prototypes ever since my
>compiler (TurboC) supported them.  The sooner everyone starts using
>them, the sooner we can forget about the obsolete crap.

My point was not that prototypes should not be used, but simply
that at this time many compilers don't support them. It's true
that Turbo-C has prototypes for years now, but that doesn't help
if I have to compile on 386/ix, SINIX (don't worry if you don't
know that :-)), XENIX-286, ...
-- 
Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83

peter@ficc.ferranti.com (Peter da Silva) (06/28/90)

In article <16980@haddock.ima.isc.com> karl@haddock.ima.isc.com (Karl Heuer) writes:
> An ANSI-to-Classic conversion tool could do the job, but
> I haven't heard of one that inserts the necessary casts.

And it still wouldn't take care of

	execl("/bin/foo", "foo", "bar", (char *)NULL);

Casting NULL is always going to be with us.

> Karl W. Z. Heuer (karl@kelp.ima.isc.com or ima!kelp!karl), The Walking Lint

Oh well, lint has always had a problem with varargs routines. :->
-- 
Peter da Silva.   `-_-'
+1 713 274 5180.
<peter@ficc.ferranti.com>

levy@mtcchi.uucp (2656-Daniel R. Levy(0000000)0000) (07/02/90)

peter@ficc.ferranti.com (Peter da Silva) writes:

>In article <16980@haddock.ima.isc.com> karl@haddock.ima.isc.com (Karl Heuer) writes:
>> An ANSI-to-Classic conversion tool could do the job, but
>> I haven't heard of one that inserts the necessary casts.

>And it still wouldn't take care of

>	execl("/bin/foo", "foo", "bar", (char *)NULL);

Devil's advocate: why not?  All it has to know is that all arguments to
execl must be of type char *.  By adding some more elaborate smarts to the
converter, this could be extended to many other (but, granted, not literally
ALL) varargs functions.  And in the cases of the functions the converter could
not handle, it could warn the user to fix them up manually.
-- 
Daniel R. Levy
Memorex Telex Corporation
Naperville IL
..!uunet!tellab5!mtcchi!levy

rgh@inmet.inmet.com (07/05/90)

You can have typed null pointers without having to declare a macro
for each one:

#define NIL(type) ((type)0)

It's a matter of taste whether 

    NIL(char *)

looks better than

    ((char*)0)


Randy Hudson   rgh@inmet.inmet.com