home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20061 < prev    next >
Encoding:
Internet Message Format  |  1993-01-22  |  2.5 KB

  1. Xref: sparky comp.lang.c:20061 comp.std.c:3397
  2. Path: sparky!uunet!spool.mu.edu!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
  3. From: torek@horse.ee.lbl.gov (Chris Torek)
  4. Newsgroups: comp.lang.c,comp.std.c
  5. Subject: Re:  can a pointer cast to a void* be compared to (void*)0 ?
  6. Followup-To: comp.std.c
  7. Date: 22 Jan 1993 10:34:25 GMT
  8. Organization: Lawrence Berkeley Laboratory, Berkeley CA
  9. Lines: 49
  10. Message-ID: <28570@dog.ee.lbl.gov>
  11. References: <C0nzI2.CqM@cs.psu.edu> <1993Jan16.015545.26580@thunder.mcrcim.mcgill.edu> <alien.02pi@acheron.amigans.gen.nz>
  12. NNTP-Posting-Host: 128.3.112.15
  13.  
  14. In article <alien.02pi@acheron.amigans.gen.nz> alien@acheron.amigans.gen.nz
  15. (Ross Smith) writes:
  16. >Now a couple of questions of my own. I know (or think I know) that the
  17. >standard does not require pointers to functions to be castable to/from any
  18. >other pointer type.
  19.  
  20. Yes (or rather, the effect is undefined or unspecified or otherwise
  21. left dangling).  (I should go dig out my ANSI C standard box and unpack
  22. it someday :-) )
  23.  
  24. >(1) Are pointers to different types of function required to be castable to
  25. >each other?
  26.  
  27. Yes; however, the result is up to the implementation.  Your only guaranteee
  28. is that if the result is then cast *back* to the `correct' type, you
  29. get back the original pointer-to-function.  I know for certain that
  30. the final result can be used to *call* that function; I am somewhat
  31. less certain that it must compare equal to the original pointer.
  32.  
  33. >(2) If a compiler does allow casting between "ordinary" pointers and function
  34. >pointers, does the rule about null pointers remaining null still apply?
  35.  
  36. (By `ordinary' I assume you mean `data' or `object' pointers.)
  37. It does not, so the, ah, point, is moot.
  38.  
  39. Note, however, that the null pointer constant can be cast or assigned
  40. to a type of the form `pointer to function ...'; the result is a null
  41. pointer of that type, which should compare equal to another null pointer
  42. constant.
  43.  
  44. Another question (which belongs in comp.std.c; note followup header):
  45. do null pointer constants, cast or assigned to pointers to functions
  46. of different types, then to a common type, compare equal?  For instance:
  47.  
  48.     typedef int (*f1)(int);        /* function type #1, e.g., abs */
  49.     typedef double (*f2)(double);    /* function type #2, e.g., sin */
  50.     typedef void (*f3)(void);    /* function type #3 */
  51.  
  52.     f1 a;
  53.     f2 b;
  54.  
  55.     a = NULL;
  56.     b = NULL;
  57.  
  58.     (f2)a == b    /* must this give 1? */
  59.     (f3)a == (f3)b    /* how about this? (should be the same, I imagine) */
  60. -- 
  61. In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
  62. Berkeley, CA        Domain:    torek@ee.lbl.gov
  63.