pointer checking

Brandon Allbery allbery at ncoast.UUCP
Thu Feb 25 08:41:54 AEST 1988


As quoted from <11878 at brl-adm.ARPA> by rbj at icst-cmr.arpa (Root Boy Jim):
+---------------
(Some ignorant idiot comments:)
|    If you look at the source you will see that the authors blatenly
|    compaired pointers to zero without thinking what happens if
|    sizeof(ptr) != sizeof(int).  I know that C does not gripe if
|    you do something like this, but can't people program better?
+---------------

Jim's right:  this is legal, although I would call it stylistically incorrect.
This works fine as long as it is a literal zero; the definition of the C
language is such that the constant 0 is promoted to a pointer of the required
type.  A cast is only required for (inherently non-portable) nonzero constants
or for variables (zero or not).  Me, I use the cast always, to make sure the
reader of my code knows that I'm talking pointers.

+---------------
| Does your compiler barf? If so, it is incorrect.
+---------------

WHICH is incorrect?  Ncoast's compiler barfs on perl... mainly because it
tickles a bug with relational operators.  Normally this shows up as a warning
"illegal pointer/integer combination, op =" when I use something like:

	if ((win = newwin(0, 0, 0, 0)) != (WINDOW *) 0)

using curses -- the fix is an explicit cast (of course, "newwin()" is defined
as WINDOW *; it still has to be cast to avoid the warning!), but in the case of
perl, in arg.c there is some code:

	value = (double) (var != var);		/* paraphrased */

which gets "compiler error: no table entry for op =" unless I phrase it as

	value = (double) ((int) (var != var));

Actually, this is one of three compiler bugs; nethack (of all things) shows
all three.  Except that perl may have tickled a REAL weird one, since with
return values always going through ints by the time yylex has returned to
yyparse the token number has somehow been truncated into a char....

Anyone got a WORKING computer to give us?  ;-) ;-(
-- 
	      Brandon S. Allbery, moderator of comp.sources.misc
       {well!hoptoad,uunet!hnsurg3,cbosgd,sun!mandrill}!ncoast!allbery
KABOOM!!! Worf: "I think I'm sick." LaForge: "I'm sure half the ship knows it."



More information about the Comp.sources.bugs mailing list