More NULL questions

Bob Devine devine at shodha.dec.com
Sat Oct 7 05:05:08 AEST 1989


In article <5950001 at hpldola.HP.COM>, jg at hpldola.HP.COM (Joe Gilray) writes:
> 1) what is the danger with using
>          struct thing *ptr;
>          if (ptr != NULL) ...
>          as opposed to:
>          if (ptr != (struct thing *)NULL) ...

  No danger.  The comparison of `ptr' with NULL causes the NULL
to be converted to the correct type.  Look at the binary ops conversions.
I consider it good coding style to put the cast there; it lets the
future reader understand the code with less puzzlement.

> 2) Is there danger using
>          int a, b;
>          my_func(a, NULL, b);
>    as apposed to
>          my_func(a, (struct thing *)NULL, b);

  You are in trouble if you do not use function prototypes
if your system's pointer representation is not the same size
as an int.  Moreover if the pointer has some strange bit
pattern representation then the NULL may not be passed correctly.

  Other places where a conversion takes place without the
explicit cast are: in returns, assignments, and all ops.

Bob Devine



More information about the Comp.lang.c mailing list