Article View: comp.std.c
Article #2492Re: int (*f)(void) = 0; int (*f)(void) = (void *) 0;
From: Chris Torek
Date: Tue, 26 Feb 1991 22:17
Date: Tue, 26 Feb 1991 22:17
44 lines
1893 bytes
1893 bytes
In article <cechew.667604378@sol1> cechew@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@ee.lbl.gov
Technical Details
Message-ID:
<10327@dog.ee.lbl.gov>Path:
arc1-ca.pugleaf.net!utzoo!mnetor!tmsoft!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!bcm!dimacs.rutgers.edu!rutgers!ucsd!dog.ee.lbl.gov!elf.ee.lbl.gov!torekReferences:
<cechew.667604378@sol1>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!mnetor!tmsoft!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!bcm!dimacs.rutgers.edu!rutgers!ucsd!dog.ee.lbl.gov!elf.ee.lbl.gov!torek From: torek@elf.ee.lbl.gov (Chris Torek) Newsgroups: comp.std.c Subject: Re: int (*f)(void) = 0; int (*f)(void) = (void *) 0; Keywords: function pointers, null pointers Message-ID: <10327@dog.ee.lbl.gov> Date: 26 Feb 91 22:17:50 GMT References: <cechew.667604378@sol1> Reply-To: torek@elf.ee.lbl.gov (Chris Torek) Organization: Lawrence Berkeley Laboratory, Berkeley Lines: 44 X-Local-Date: Tue, 26 Feb 91 14:17:50 PST