Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!columbia!rutgers!dayton!viper!john From: john@viper.UUCP (John Stanley) Newsgroups: news.software.b,comp.lang.c Subject: Re: News for Xenix on PC AT ? Message-ID: <889@viper.UUCP> Date: Fri, 24-Apr-87 16:36:03 EST Article-I.D.: viper.889 Posted: Fri Apr 24 16:36:03 1987 Date-Received: Sun, 26-Apr-87 01:12:31 EST References: <18346@ucbvax.BERKELEY.EDU> <145@sds.UUCP> <17005@sun.uucp> <146@sds.UUCP> <17245@sun.uucp> Reply-To: john@viper.UUCP (John Stanley) Organization: DynaSoft Systems Lines: 41 Xref: mnetor news.software.b:540 comp.lang.c:1885 In article <17245@sun.uucp> guy%gorodish@Sun.COM (Guy Harris) writes: >> Why is "if (charptr == 0)" NOT identical to "if ((int)charptr == 0)" ??? > >a C compiler *must* properly handle "if (charptr == 0)". It MUST >not do an integer comparison if that would give a different result >from a pointer comparison. In effect, it must convert the "0" to a >character pointer of the appropriate type. > >On the other hand, *your* example converts the *pointer* to an >*integer* before doing the comparision, which is NOT correct. > Correct, but more importantly, casting the pointer to an int will truncate the pointer to whatever the number of bits an int would have on that system. This means, on a machine with 16 bit int's and 32 bit pointers, that any pointer which lies on a 64k boundry would test as equal to NULL which is obviously -w.r.o.n.g-. > > The C language lacks a token like Pascal's "nil", so a >constant expression with the value 0 must do double duty as a >representation of a null pointer. Wrong... Ever hear of something called "NULL"? K&R, Page 97: "We write NULL instead of zero, however, to indicate clearly that this is a special value for a pointer."... >> (!charptr) isn't portable > >Yes, it is. Although you may find one or two compilers which balk at this construct, it is correct on all "architectures". The compiler writers just need to fix their implementations on this one... --- John Stanley (john@viper.UUCP) Software Consultant - DynaSoft Systems UUCP: ...{amdahl,ihnp4,rutgers}!{meccts,dayton}!viper!john