What C compilers have non-zero null pointers?

Leo de Wit leo at ehviea.ine.philips.nl
Wed Jul 18 21:08:24 AEST 1990


In article <1990Jul17.123627.1932 at druid.uucp> darcy at druid.uucp (D'Arcy J.M. Cain) writes:
    []
|However I always use NULL for two reasons.  Broken compilers on brain dead
|CPUs and if NULL is defined as "(void *)0" then it tests for accidentally
|testing a NULL pointer against a non pointer variable.  For example:
|    int a = 0;
|    if (a == NULL)
|        do(something);
|If tested against 0 the compiler won't complain but it will complain if it
|is tested against (void *)0.  At least GNU C complains.  In other words, use
|NULL not because 0 may not be the NULL pointer but because NULL can't be
|anything else.

For much the same reason I always use explicit casts for null pointers;
this also catches unadvertent assignments or comparisions to a pointer
of a different type, and has the additional advantage that null
pointers as parameters have the same "appearance"; you don't have to
develop different habits of treating pointers. As an example:

    if (fgets(inbuf,sizeof(inbuf),fp) != (char *)0) ....

but also:

    execl("/bin/ls","ls",(char *)0);

Another advantage is that you see immediately from the program text
what kind of pointer is expected at some stage; in production code, and
especially in the maintenance phase, this may prevent a lot of type
lookups.

    Leo.



More information about the Comp.lang.c mailing list