Path: utzoo!attcan!uunet!husc6!mailrus!ames!haven!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn ) Newsgroups: comp.lang.c Subject: Re: pointers, tests, casts Message-ID: <8961@smoke.BRL.MIL> Date: 23 Nov 88 06:09:34 GMT References: <11130@dartvax.Dartmouth.EDU> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 17 In article <11130@dartvax.Dartmouth.EDU> Eric.J.Bivona@Dartmouth.EDU writes: >I have a question about tests on pointers, ... if ( !ptr ) and if ( ptr == 0 ) are both perfectly valid ways to test for a null pointer. You can explicitly cast the 0 to the proper type, but it's not necessary. Explicit casting of 0 (or NULL, defined in several headers) is required only when the compiler could mistake the 0 for an int constant rather than the intended null pointer constant. The main place this can occur is in the arguments to a function when no prototype is in scope, e.g. execl( "/bin/sh", "-sh", "-i", 0 ); (This one is even worse since it's a variable-argument function, so not even a prototype would help.) The bug typified by this is seen in a LOT of code ("well it worked on MY machine!").