delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/01/11/08:29:32

From: kagel AT quasar DOT bloomberg DOT com
Date: Thu, 11 Jan 1996 08:15:13 -0500
To: enrico AT max DOT tiac DOT net
Cc: dunder AT nyongwa DOT montreal DOT qc DOT ca, djgpp AT delorie DOT com
Subject: Re: (none)
Reply-To: kagel AT quasar DOT bloomberg DOT com

   Date: Wed, 10 Jan 1996 19:01:02 -0500 (EST)
   From: Cuthalion / Sliced Bread <enrico AT max DOT tiac DOT net>
   Cc: dunder AT nyongwa DOT montreal DOT qc DOT ca, djgpp AT delorie DOT com

   On Wed, 10 Jan 1996 kagel AT quasar DOT bloomberg DOT com wrote:

   > 	if (!my_pointer)
   > 		my_pointer = (my_struct_t *)malloc( sizeof (my_struct_t) ); 
   > 
   > Or BETTER CODE but equivalent:
   > 
   > 	if (my_pointer == (my_struct_t *)NULL)
   > 		...

	   You shouldn't need to cast NULL, I don't think...  

	   Either way, even better than that would be:
	   if (NULL == my_pointer) ...
	   since this will give you a compiler error if you make a typo and 
   only use one equal sign.  (Otherwise, it is an assignment, which can 
   cause no end of problems.  It's also a PAIN to find, when you do do it.)

I like that!  I'm programming 15 years and never thought of that one.  Thanks.

You are right NULL is explicitly compatible with any pointer type, however, I
always cast NULL and 0 for documentation purposes.  This helps remind me of my
pointer types and makes it much easier for someone else to pick up my code.  So
in the sense that we all strive for code which is easier to maintain, and that 
such code is BETTER code, I would, following your suggestion, write:

	if ((my_struct_t *)NULL == my_pointer) ...

-- 
Art S. Kagel, kagel AT quasar DOT bloomberg DOT com

A proverb is no proverb to you 'till life has illustrated it.  -- John Keats

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019