10 commandments and the NULL pointer?

Peter Holzer hp at vmars.tuwien.ac.at
Fri Nov 9 00:05:44 AEST 1990


einari at rhi.hi.is (Einar Indridason) writes:
>#include <whatever_needs_to_be_included.h>

>int do_something(int *par1, double *par2, char *par3)
>{
>	/* do something with those pointers/parameters */
>	/* return some value to tell how well we did */
>}


>main()
>{
>	.....

>	x = do_something(NULL, NULL, NULL);
>	/* is            ^^^^  ^^^^  ^^^^  this right or should I write: */

>	x = do_something( (int *)NULL, (double *)NULL, (char *)NULL );

>	/* which form should I use?  (that is, should I cast the NULL value */
>	/* to int pointer, double pointer, char pointer?) */
>}
>-----------------------------------------

If you are using ANSI C, then the above form is allright,
because the compiler knows from the prototype above the types of
the pointers and inserts the casts by itself.

If you (or someone else) want to port your program to an old
compiler (and many of them are still in use) and change the
declaration of do_something to:

int do_something (par1, par2, par3)
	int	* par1;
	double	* par2;
	char	* par3;
{
/* ... */
}

you will need the casts because the compiler does not check (much
less convert) any
arguments. 
--
|    _  | Peter J. Holzer                       | Think of it   |
| |_|_) | Technical University Vienna           | as evolution  |
| | |   | Dept. for Real-Time Systems           | in action!    |
| __/   | hp at vmars.tuwien.ac.at                 |     Tony Rand |



More information about the Comp.lang.c mailing list