home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:20061 comp.std.c:3397
- Path: sparky!uunet!spool.mu.edu!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
- From: torek@horse.ee.lbl.gov (Chris Torek)
- Newsgroups: comp.lang.c,comp.std.c
- Subject: Re: can a pointer cast to a void* be compared to (void*)0 ?
- Followup-To: comp.std.c
- Date: 22 Jan 1993 10:34:25 GMT
- Organization: Lawrence Berkeley Laboratory, Berkeley CA
- Lines: 49
- Message-ID: <28570@dog.ee.lbl.gov>
- References: <C0nzI2.CqM@cs.psu.edu> <1993Jan16.015545.26580@thunder.mcrcim.mcgill.edu> <alien.02pi@acheron.amigans.gen.nz>
- NNTP-Posting-Host: 128.3.112.15
-
- In article <alien.02pi@acheron.amigans.gen.nz> alien@acheron.amigans.gen.nz
- (Ross Smith) writes:
- >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.
-
- Yes (or rather, the effect is undefined or unspecified or otherwise
- left dangling). (I should go dig out my ANSI C standard box and unpack
- it someday :-) )
-
- >(1) Are pointers to different types of function required to be castable to
- >each other?
-
- Yes; however, the result is up to the implementation. Your only guaranteee
- is that if the result is then cast *back* to the `correct' type, you
- get back the original pointer-to-function. I know for certain that
- the final result can be used to *call* that function; I am somewhat
- less certain that it must compare equal to the original pointer.
-
- >(2) If a compiler does allow casting between "ordinary" pointers and function
- >pointers, does the rule about null pointers remaining null still apply?
-
- (By `ordinary' I assume you mean `data' or `object' pointers.)
- It does not, so the, ah, point, is moot.
-
- Note, however, that the null pointer constant can be cast or assigned
- to a type of the form `pointer to function ...'; the result is a null
- pointer of that type, which should compare equal to another null pointer
- constant.
-
- Another question (which belongs in comp.std.c; note followup header):
- do null pointer constants, cast or assigned to pointers to functions
- of different types, then to a common type, compare equal? For instance:
-
- typedef int (*f1)(int); /* function type #1, e.g., abs */
- typedef double (*f2)(double); /* function type #2, e.g., sin */
- typedef void (*f3)(void); /* function type #3 */
-
- f1 a;
- f2 b;
-
- a = NULL;
- b = NULL;
-
- (f2)a == b /* must this give 1? */
- (f3)a == (f3)b /* how about this? (should be the same, I imagine) */
- --
- In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
- Berkeley, CA Domain: torek@ee.lbl.gov
-