More NULL questions

Richard O'Keefe ok at cs.mu.oz.au
Sat Oct 7 19:20:24 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
>       struct thing *ptr; ... if (ptr != (struct thing *)NULL) ...

None.  You can even use    ... if (ptr) ...

> 2) Is there danger using
>       int a, b; ... my_func(a, NULL, b);
>    as Opposed to
>       int a, b; ... my_func(a, (struct thing *)NULL, b);
>    when
>    a) you are using function prototypes?

No.  If there is a prototype in scope, passing an argument is like
assignment, NULL will be converted to the appropriate pointer type.

>    b) you are NOT using function prototypes (like me)?

Yes.  You will get an int, which may not be the same size as a pointer,
and if it is, may not be the same bit pattern as (struct thing *)NULL.

It isn't just NULL.  With some C compilers, and on some machines,
the cast in
	struct thing *ptr; ...
	if (fwrite((char *)ptr, sizeof *ptr, 1, stream) != 1) ...
is absolutely necessary.  (The cast is (void *) in ANSI C, and is
not needed if you have the prototype of fwrite() in scope.)



More information about the Comp.lang.c mailing list