use of if (!cptr) and if (cptr), where cptr is a *)

der Mouse mouse at mcgill-vision.UUCP
Wed Aug 9 19:11:06 AEST 1989


In article <660046 at hpclwjm.HP.COM>, walter at 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 at larry.mcrcim.mcgill.edu



More information about the Comp.lang.c mailing list