pointers, tests, casts

William E. Davidsen Jr davidsen at steinmetz.ge.com
Wed Nov 30 01:47:39 AEST 1988


In article <8961 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
| In article <11130 at dartvax.Dartmouth.EDU> Eric.J.Bivona at Dartmouth.EDU writes:
| >I have a question about tests on pointers, ...
| 
| 	if ( !ptr )
| and
| 	if ( ptr == 0 )
| are both perfectly valid ways to test for a null pointer.  You can
| explicitly cast the 0 to the proper type, but it's not necessary.

Doug, as usual you are correct, but I have to point out that
	if (ptr == NULL)
also works, usually generates the same code, and gives a much better
idea of what the code is doing. I'm sure that some of the new readers of
this group would not quickly grasp the meaning of your first example,
and I'm not sure about the second. I just covered this topic in a C
course I'm teaching, and I am always amazed at how easily new C
programmers are confused by shorthand form which "mean the same thing."
-- 
	bill davidsen		(wedu at ge-crd.arpa)
  {uunet | philabs}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me



More information about the Comp.lang.c mailing list