Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!columbia!rutgers!ames!ucbcad!zen!cory.Berkeley.EDU!waterman From: waterman@cory.Berkeley.EDU (T.S. Alan Waterman) Newsgroups: comp.sys.amiga Subject: Re: Manx C Message-ID: <3211@zen.berkeley.edu> Date: Thu, 6-Aug-87 15:01:42 EDT Article-I.D.: zen.3211 Posted: Thu Aug 6 15:01:42 1987 Date-Received: Sat, 8-Aug-87 12:00:44 EDT References: <4540@jade.BERKELEY.EDU> <1836@vax135.UUCP> <4595@jade.BERKELEY.EDU> <3199@zen.berkeley.edu> <17827@amdcad.AMD.COM> Sender: news@zen.berkeley.edu Reply-To: waterman@cory.Berkeley.EDU.UUCP (T.S. Alan Waterman) Distribution: comp Organization: University of California, Berkeley Lines: 97 Keywords: look at the assembly. Summary: I admit it's confusing, but.. In article <17827@amdcad.AMD.COM> tim@amdcad.UUCP (Tim Olson) writes: >+----- >No. Mike is right. From K&R, section 7.7 (Equality Operators): > > "... A pointer to which 0 has been assigned is guaranteed not to > point to any object, *and will appear to be equal to 0*; in > conventional usage, such a pointer is considered to be null." > [emphasis mine] There are many kinds of '0' (short, long, pointer, floating-point..) and saying "equal to 0" leaves things open to Manx's type conversion. Manx has a bug in that it likes to leave constants as untouched as possible. [argument mine] > > From the X3J11 ANSI C Draft, section 3.2.2.3 (Pointers): > > "... An integral constant expression with the value 0, or such an ^^^^^^^^ but how big is an integer?? (we've been through this before!!) > expression cast to type void, is called a null pointer constant. > If a null pointer constant is assigned to or compared for > equality to a pointer, the constant is converted to a pointer of > that type. Such a pointer, called a *null pointer*, is > guaranteed not to point to any object or function." > >Any C compiler which doesn't perform in this manner is broken. You're right--it's broken, but now we all know that Manx shortens the pointer instead of extending the constant, so we have two choices: 1) live with it, or 2) live with it for now, and hope Goodnow fixes it soon! What I don't get is this-- (!ptr) seems more straightforward, you never have to worry about types, and it makes smaller code than (ptr == 0) anyway. Why use the second expression? (with pointers, at least.) > >[...] !ptr is *exactly* equivalent to ptr==0. K&R, section 7.2: > > "... The result of the logical negation operator ! is 1 if the > value of its operand is 0, 0 if the value of its operand is > non-zero. ... It is applicable to any arithmetic type *or to > pointers*." > [emphasis mine] This is very true, but "!ptr is *exactly* equivalent to ptr==0" is misleading. (ptr == 0) does type checking and conversion, (!ptr) doesn't. > > From the X3J11 ANSI C Draft, section 3.3.3.3 (Unary Operators) > > "... The result of the logical negation operator ! is 0 if the > value of its operand is nonzero, 1 if the value of its operand > is 0. ... The expression !E is equivalent to (0==E)." > >Sorry for the diatribe, but us C police want to eradicate any >disinformation as soon as possible so that it doesn't corrupt young >minds ;-) ;-) ;-) > > -- Tim Olson > (tim@amdcad.amd.com) > > "C-Police 'R' Us" I really don't want to turn this into a war, but I have to disagree, at least in principle. The comparison operator '==' only applies (and I think only should apply) if the objects being compared ARE OF THE SAME TYPE!!!! If your 0 happens to be of the same type as your pointer, fine; but, on the other hand, if it's not, something's going to get type converted. If both arg's are numeric, this wont be a problem, because the 'bigger' type will win, and the other one will get converted (try comparing int's and reals). Pointers, though, confuse the poor compiler, because it's not sure what you really meant to do. Try comparing a pointer and a floating-point real. Before you flame, I'll admit that it makes no sense to do that. But then, it really doesn't make sense to compare a pointer with a constant of a different size, either, becuase the compiler doesn't (but probably should) know what to do with it. It's awfully inconvienient (maybe that's putting it REAL mildly) to go redefine NULL, or go around comparing to 0L if you don't want to use the ! operator. But the compiler is slightly in that it munches the pointer instead of the int. (Or, if you want to be a purist about it and not have have to worry, use Lettuce C, where ALL the ints are 32 bits :^) Type comparisons are always a pain in the rear, but they get especially bad with constants (like 0), beacause (this) compiler assumes you knew what you meant when you typed 0 (and not 0L). Use a NULL that's the same size as your pointer. Big Brother's been sleeping--careful not to wake him up... --TS (sorry to be so long-winded -- it's a bad habit)