Path: utzoo!utgpu!water!watmath!clyde!att-cb!att-ih!chinet!dag From: dag@chinet.UUCP (Daniel A. Glasser) Newsgroups: comp.lang.c Subject: Re: Header problems Summary: don't "#define NULL 0", use "#define NULL ((void *)0)" instead Message-ID: <3351@chinet.UUCP> Date: 8 Mar 88 15:55:27 GMT References: <2550049@hpisod2.HP.COM> <7412@brl-smoke.ARPA> Reply-To: dag@chinet.UUCP (Daniel A. Glasser) Organization: Chinet - Public Access Unix Lines: 47 In article <7412@brl-smoke.ARPA> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) writes: >NULL is defined in (for historical compatibility) and in > (where it really belongs). If you need to use the symbol, >you should include one or both of these two headers. Of course NULL >is only a "convenience" macro, because it is clear how to write any >type of null pointer constant in the language proper. It was included >in the propsoed standard because so much code uses it, not because it >is technically necessary for portability. > >The implementor should put something like the following in each header: > #define NULL 0 > > >P.S. These are my view, not necessarily X3J11's. Your use of NULL === 0 promotes unportable code, and is considered to be bad programming style in a world where portability across multiple machine architectures (ie, PDP-11, VAX-11, M680x0, I80x86 and Z800x) is required for commercial reasons. On machines were sizeof int != sizeof(void *), the above definition will not work on older style function calls (without prototypes) or in var-arg situations. Requiring sizeof int == sizeof(void *) is not a viable solution. A better way to do it, in each place you want to define NULL, is: #ifndef NULL #define NULL ((void *)0) #endif On older compilers, replace 'void' with 'char'. This allows the use of a short NULL representation when the compiler is smart enough to use it but forces passing of NULL in argument lists as a pointer sized object. What the C language guarentees is not that 0===NULL, but that a constant 0 can be assigned/compared to a pointer without warning or error regardless of pointer representation and compare as equal to the NULL pointer. In cases where the compiler is unable to determine if an int or type * is being passed, 0 is passed as an int. The use of the name NULL in the preprocessor does not affect this. ('===', as used above, means exactly equivilant) -- Daniel A. Glasser ...!ihnp4!chinet!dag || ...!ihnp4!mwc!dag || ...!ihnp4!mwc!gorgon!dag One of those things that goes "BUMP!!! (ouch!)" in the night.