Newsgroup: comp.os.minix


Article 6000 of comp.os.minix:
Path: icdoc!tsun7.doc.ic.ac.uk!zmact61
>From: zmact61@tsun7.doc.ic.ac.uk.doc.ic.ac.uk (D Spinellis)
Newsgroups: comp.os.minix
Subject: Re: Major criticism in a high-pitched voice after many sleepless nights
Summary: Programs should cast NULL to the appropriate type
Keywords: NULL cast
Message-ID: <1341@gould.doc.ic.ac.uk>
Date: 31 Oct 89 18:51:07 GMT
References: <386@my5.sm.luth.se>
Sender: news@doc.ic.ac.uk
Reply-To: dds@cc.ic.ac.uk (Diomidis Spinellis)
Organization: Imperial College Department of Computing
Lines: 50
Content-Length: 2045
In article <386@my5.sm.luth.se> Johan Nordlander <johan@sm.luth.se> writes:
>The following is a selection of the most prominent bugs found during the
>porting of MINIX/ST to the Motorola/VME lab equipment used at our
>departement.
[...]
>In K&R it is stated that NULL is a special value for a pointer. This
>implies that on a 16-bit machine with 32-bit pointers, passing NULL as
>an argument to a function should result in 4 bytes being pushed. In
>include/stdio.h NULL is defined as 0, making it a 16-bit int when it
>comes to argument passing. 

Defining NULL as 0 is a perfectly correct decision.  The problem lies
in the program calling the function with NULL as a parameter without
casting it to the appropriate type.  In Classic C the cast is always
needed, programs that don't use it are WRONG.  Some PC compiler vendors
have tried to bodge the definition of NULL to make wrong programs
behave correctly by defining NULL as 0L.  In past discussions in
comp.lang.c this was considered to be A Bad Thing.  In ANSI C a cast is
not needed if a function prototype has been given for the function
called and (in the case of functions taking a variable number of
arguments) the argument passed is one of the arguments in the
prototype.

Examples:
Classic C
---------
func is a function expecting a character pointer.
func(0);			Wrong (no cast)
func((char *)0);		Correct
func(NULL);			Wrong (no cast)
func((char *)NULL);		Correct

ANSI C
------
void func(char *arg);
func(NULL);			Correct
func(0);			Correct
func()char *)0);		Correct

void bar(char *arg, ...);
bar(0);				Correct
bar(0, 0);			Wrong (args beyond those declared need casting)
bar(0, (char *)0);		Correct

Diomidis
--
Diomidis Spinellis                  Internet:                 dds@cc.ic.ac.uk
Department of Computing             BITNET:                   dds@cc.ic.ac.uk
Imperial College                    UUCP:   ...!cernvax!cc.imperial.ac.uk!dds
London SW7 2BZ                      JANET:                    dds@uk.ac.ic.cc




Newsgroup comp.os.minix contents
Newsgroup list
Diomidis Spinellis home page

Creative Commons License Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-Share Alike 3.0 Greece License.