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

  1. Path: sparky!uunet!ulowell!news.bbn.com!olivea!charnel!psgrain!ee.und.ac.za!tplinfm
  2. From: barrett@lucy.ee.und.ac.za (Alan Barrett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: (cast) proposal
  5. Message-ID: <1k5mnuINNntr@lucy.ee.und.ac.za>
  6. Date: 27 Jan 93 10:04:46 GMT
  7. References: <1993Jan26.135214.9306@dxcern.cern.ch>
  8. Organization: Elec. Eng., Univ. Natal, Durban, S. Africa
  9. Lines: 62
  10. NNTP-Posting-Host: lucy.ee.und.ac.za
  11.  
  12. In article <1993Jan26.135214.9306@dxcern.cern.ch>,
  13. burow@dxcern.cern.ch (Burkhard Burow) writes:
  14. > -|>That's why I need a cast for saying:
  15. > -|> 'This is a pointer to a function.
  16. > -|>  I don't give a flying f*** about the type of argument the
  17. > ->|  function returns.'
  18.  
  19. As Chris Torek has explained, with his usual eloquence, there is no such
  20. thing in C.
  21.  
  22. > RFC: Why doesn't ANSI C provide this?
  23.  
  24. Because it wouldn't be very useful.  An implementation is free to use
  25. different calling conventions for functions that take different types
  26. of args or that return different types.
  27.  
  28. (void(*)()) or (void(*)(void)) is about as close as you can get.
  29.  
  30. I went back to to your original posting (which had a misleading subject
  31. line that suggested that it was relevant only to a DEC implementation),
  32. in which you asked about this:
  33.  
  34. % My application generates 'wrapper' routines which pass along functions
  35. % to other routines. The wrapper routines do not know the type of the
  36. % functions they are passing along.
  37. %
  38. % Simplified e.g.
  39. %
  40. % void q ( int (*c)() ); 
  41. % void __q( void ( *a)() )
  42. % {
  43. %   q( a );
  44. % }
  45.  
  46. The problem here is that the type of the arg that __q passes to q does
  47. not match the declaration of q.  Since you say that the writer of the
  48. __q function does not know how q is declared, you cannot just cast the
  49. arg a to (int(*)()) inside __q.
  50.  
  51. The effect of calling a function through a pointer of the wrong type is
  52. undefined, but if you can live with that and you know that your C
  53. implementation permits this dubious practice, then you can say this:
  54.  
  55. typedef void (*pfv)();    /* pointer to function taking unknown args
  56.              * and returning void */
  57. typedef int (*pfi)();    /* pointer to function taking unknown args
  58.              * and returning int */
  59. void q(pfi c);        /* q takes a pfi and returns void */
  60. void __q(pfv a)
  61. {
  62.     /* We have a pfv that we need to pass to q, but we don't know what
  63.      * type q wants to receive, and we don't know what type q returns.
  64.      * We cheat by casting q into a type that we do know about, and
  65.      * calling the function through the resulting pointer.
  66.      * The behaviour is undefined, but we hope that the implementation
  67.      * is very friendly. */
  68.     ((pfv)q) (a);
  69. }
  70.  
  71. --apb
  72. Alan Barrett, Dept. of Electronic Eng., Univ. of Natal, Durban, South Africa
  73. RFC822: barrett@ee.und.ac.za
  74.