Mail Archives: djgpp/1996/01/14/04:36:41
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 -