pointers, tests, casts

Doug Gwyn gwyn at smoke.BRL.MIL
Thu Dec 1 02:47:20 AEST 1988


In article <12690 at steinmetz.ge.com> davidsen at crdos1.UUCP (bill davidsen) writes:
-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."

I wasn't recommending anything other than if(ptr==NULL) (which requires
that the programmer arrange for NULL to be properly defined first, but
I always do arrange that for my code).  In fact I wasn't making any
stylistic suggestion at all, just answering a specific question.



More information about the Comp.lang.c mailing list