int (*f)(void) = 0; int (*f)(void) = (void *) 0;

Chris Torek torek at elf.ee.lbl.gov
Wed Feb 27 09:17:50 AEST 1991


In article <cechew.667604378 at sol1> cechew at sol1.cs.monash.edu.au (Earl Chew)
writes:
>The standard states that 0 and (void *) 0 are `null pointers'.

More precisely, they are `null pointer constants'.  While (void *)0 is
a null pointer, 0 is not a null pointer; it is merely a null pointer
constant (as well as the integer constant 0).

>Does this mean that they are equivalent in all contexts?

No; but:

>Specifically:
>	int (*f)(void) = 0;
>My understanding is that this is permissible.

Yes.

>	int (*f)(void) = (void *) 0;
>I am unsure whether this is permissible.

It is.  The integral constant zero is peculiar in that it has two
meanings: integer 0, or generic nil pointer constant.  (void *)0 is
also peculiar: it is both a generic nil pointer constant and a specific
nil pointer (nil pointer to void).

The difference between the two manifests in several contexts, including:

	printf("%d\n", (int)sizeof(0));
vs
	printf("%d\n", (int)sizeof((void *)0));

or, given
	extern void f();		/* no prototype! */

	f(0);
vs
	f((void *)0);

Each statement in each pair will do something different from the other
in its pair, at least on some systems.
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab EE div (+1 415 486 5427)
Berkeley, CA		Domain:	torek at ee.lbl.gov



More information about the Comp.std.c mailing list