SIZEOF
Dennis Smith
quenton at ecr.UUCP
Wed Jan 23 19:34:21 AEST 1985
The problem of passing 0 for a null pointer (as a parameter), and
the solution of "#define NULL ...", as pointed out by P.Curran,
is valid. However, the use of -
#define NULL ((char *)0)
although portable will cause many compilers to complain about
differing pointer types, and will also cause lint to generate many
additional useless messages. The only generally useable solution
that I know of is -
#define NULL 0 /** when sizeof(xxx *) == sizeof(int) **/
#define NULL 0L /** when sizeof(xxx *) == sizeof(long) **/
This unfortunately means that the "define" must be changed whenever
the target machine/compiler/environment changes.
One possible solution for the future could be the use of -
#define NULL ((void *)0)
which seems compatable with the notation of (void *) being a generic
pointer type.
It might also be noted, although I have had no experience with them,
some compilers for certain older generations of computers, generate
pointers of differing sizes. This occurs when the machine is not
byte addressable, so that a pointer to a word aligned item might
be "n" bits long, but a pointer to a character must point to the
word and also indicate which character within the word.
This would make the even more disastrous situation of
sizeof(char *) != sizeof(int *)
making the defintion of something like NULL even more incomprehensible.
More information about the Comp.lang.c
mailing list