delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/01/14/04:36:41

Date: Sun, 14 Jan 1996 11:28:15 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: "Eric J. Korpela" <korpela AT islay DOT ssl DOT berkeley DOT edu>
Cc: djgpp AT delorie DOT com
Subject: Re: (none)

On 11 Jan 1996, Eric J. Korpela wrote:

> In article <DKzMM1 DOT KrA AT jade DOT mv DOT net>,  <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)
> 
> Is this really better?  Could option 1 fail?  I thought the C standard
> guaranteed that the top option would work.

Yes, it *is* better.  ANSI C says that the null pointer (for any type) is 
a zero cast to a pointer to that type, but it doesn't guarantee that the 
bit pattern for the null pointer is all-zero (so that !null_ptr is 
true).  So you could write `if (my_pointer == 0)' (because the compiler 
automatically will cast 0 to the pointer type), but `if (!my_pointer)' 
may not work on some odd architectures.

- Raw text -


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