home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!comp.vuw.ac.nz!acheron.amigans.gen.nz!alien
- From: alien@acheron.amigans.gen.nz (Ross Smith)
- Newsgroups: comp.lang.c
- Subject: Re: can a pointer cast to a void* be compared to (void*)0 ?
- Message-ID: <alien.02pi@acheron.amigans.gen.nz>
- Date: 22 Jan 93 13:53:02 GMT+12
- References: <C0nzI2.CqM@cs.psu.edu> <1993Jan16.015545.26580@thunder.mcrcim.mcgill.edu>
- Distribution: world
- Organization: Muppet Labs
- Lines: 65
-
- In article <1993Jan16.015545.26580@thunder.mcrcim.mcgill.edu> mouse@thunder.mcrcim.mcgill.edu (der Mouse) writes:
- >In article <C0nzI2.CqM@cs.psu.edu>, andrew@astro.psu.edu (Andrew Wilcox) writes:
- >> char* c = 0;
- >> void* p = (void*) c;
- >> if (p == 0)
- >> printf( "yes\n" );
- >
- >> Is this program guaranteed to print "yes"?
- >
- >Ah, this asks more. You want to know if a null pointer of one type is
- >guaranteed to be a null-pointer-to-void when converted. I don't think
- >this is guaranteed. In the case of char *, the grandfather equivalence
- >of char * and void * may mean that it is, but I don't think so. (The
- >equivalence means, I think, that you could memcpy c to p and thereby
- >ensure getting a null void *. Of course, most systems will let it
- >work as you've written it, since it will usually require extra effort
- >to break it, but I don't think it's guaranteed.)
-
- Sorry, you've got it backwards. (The FAQ explains this in all its gory
- detail :-)
-
- A null pointer is required to remain a null pointer when cast to any other
- pointer type (except function pointers, which are a special case), and the
- integer zero and any null pointer are required to cast to to each other
- (except ditto). However, null pointers are *not* required to be actual
- bitwise zeros (and null pointers to different types are not required to be
- identical), so a memcpy() will *not* (necessarily) work. If a null pointer
- is not bitwise equivalent to the integer zero, the compiler must perform
- whatever magic is required to make them interchangeable in source code.
-
- In other words, any ANSI compiler is required to treat the following code
- fragments as equivalent, *regardless* of what bit patterns are used for
- pointers:
-
- int *iptr = NULL;
-
- int *iptr = 0;
-
- int *iptr;
- void *vptr = NULL;
- iptr = (int *)vptr;
-
- But in the last case, memcpy(&iptr, &vptr, sizeof(void *)) would not
- necessarily put a null pointer into iptr (in fact, sizeof(void *) and
- sizeof(int *) need not be the same).
-
- (All this, of course, assumes that I've correctly understood the FAQ, which
- I'd be the first to admit is by no means a foregone conclusion :-)
-
- Now a couple of questions of my own. I know (or think I know) that the
- standard does not require pointers to functions to be castable to/from any
- other pointer type.
-
- (1) Are pointers to different types of function required to be castable to
- each other?
-
- (2) If a compiler does allow casting between "ordinary" pointers and function
- pointers, does the rule about null pointers remaining null still apply?
-
- --
- ...... Ross Smith (Wanganui, NZ) ...... alien@acheron.amigans.gen.nz ......
- "I blame you for the moonlit sky and the dream that died with the Eagle's flight
- I blame you for the moonlit nights when I wonder why are the seas still dry
- Don't blame me, sleeping satellite" (Tasmin Archer)
- --
-