10 commandments and the NULL pointer?

Chris Torek chris at mimsy.umd.edu
Fri Nov 9 03:11:08 AEST 1990


In article <2390 at krafla.rhi.hi.is> einari at rhi.hi.is (Einar Indridason) writes:
>However one question entered my mind:  (a question that perhaps should
>be in the FAQ list, and perhaps is there?)

It is.

>int do_something(int *par1, double *par2, char *par3) ...
>	x = do_something(NULL, NULL, NULL);
>	/* is            ^^^^  ^^^^  ^^^^  this right or should I write: */

You have provided a prototype declaration (via a preceding prototype
definition) in which all three parameters are covered by prototypes.
They are therefore in assignment contexts and as a result, an integer
constant zero or same-cast-to-void (NULL expands to one of these two)
suffices, because the pointer context is supplied by the assignments.

>	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?) */

Some people prefer the latter form even when prototypes are used, as it
helps remind the reader what is going on.  Still others prefer something
like:

	/* special distinguished second parameter to DoSomething */
	#define NOMATRIX ((double *)NULL)
	...
		x = do_something(NOFOO, NOMATRIX, NOBAR);
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 405 2750)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list