Path: utzoo!utgpu!watmath!att!ucbvax!bloom-beacon!mcgill-vision!mouse From: mouse@mcgill-vision.UUCP (der Mouse) Newsgroups: comp.lang.c Subject: Re: use of if (!cptr) and if (cptr), where cptr is a *) Message-ID: <1603@mcgill-vision.UUCP> Date: 9 Aug 89 09:11:06 GMT References: <10099@mpx2.mpx.com> <660046@hpclwjm.HP.COM> Organization: McGill University, Montreal Lines: 36 In article <660046@hpclwjm.HP.COM>, walter@hpclwjm.HP.COM (Walter Murray) writes: > As a further exam[p]le of the latitude you have in writing a null > pointer constant, the following would also be legal. > if (cptr == (signed short)0.3+sizeof(char)-'\1' > +sizeof((long int*)myfunc()+3,000)-sizeof'M'); > I think any ANSI-conforming compiler will accept this statement. Leaving aside the point (on which even Chris Torek is unsure) of whether or not a cast produces a non-"constant" expression, this is valid only if sizeof(long int *) equals sizeof(int), which isn't true everywhere. Your expression also requires that myfunc() be declared (possibly implicitly) as returning something that can be cast into (long int *); this might not be true (eg, it may return a struct). Question: If it does happen that sizeof(long int *) == sizeof(int), is it correct to write "if (cptr == sizeof(long int *)-sizeof(int))"? In other words, must we compare against an integer constant expression which is guaranteed to be zero or may we get away with using one which just happens to be zero for the compilation environment in use? I'd guess it's the latter, but am curious. If I'm right, is it then possible to use this as a means of drawing a guaranteed compile-time warning if sizeof(long int *) is *not* equal to sizeof(int), or are compilers not required to warn about pointer/int mismatches like that? (Yes, I know the warning probably will not be very descriptive of the *real* problem. :-) der Mouse old: mcgill-vision!mouse new: mouse@larry.mcrcim.mcgill.edu