NULL pointers

Kent Williams kent at ncoast.UUCP
Sat Nov 15 00:34:56 AEST 1986


RE : The Use of NULL

Maybe I'm ignorant, but the use of NULL is a constant source of pain for which
it seems there is a simple solution to wit

/* local.h - include after other include modules */
#ifdef HASVOID
#define NULL ((void *)0)
#else
#define NULL 0L
#endif

. . .

If NULL is typed to have the size of the largest possible object that it
will be assigned to, it will be 'narrowed' to fit into whatever object you
are assigning it to.

As a matter of style, NULL for me seems to be something that should only be
used as a pointer object.  I get sick to my stomach every time I see

int stupid(cp)
char *cp;
{
	*cp = NULL;
}

Do they REALLY mean they want a 0 poked into the character pointed to by cp,
or did they mean cp = NULL, or what?

If you have void type, then a void pointer should be a 'universal pointer,'
i.e. assignable to any pointer variable, but un-dereference-able without a cast
to a non-void type.

Also, malloc and calloc should be of the type void pointer, so that you
don't get invalid pointer assignment complaints from compilers.  It seems
supremely asinine that Microsoft C complains about

struct nameless x;
x = malloc(sizeof(struct nameless));

All of the above suggestions should be very portable - if they're not, let me
know why.

AND another thing - why isn't it standard practice to bracket the standard
include files with the following?

/* stdio.h */
#ifndef STDIO_H_INCLUDED
#define STDIO_H_INCLUDED
/* rest of stdio.h */
#endif

This would mean that you could re-include things without complaints from
the pre-processor and compiler.

These opinions are my own, and reflect the views only of me.



More information about the Comp.lang.c mailing list