home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / std / c / 3222 < prev    next >
Encoding:
Text File  |  1992-12-17  |  1.9 KB  |  48 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!haven.umd.edu!decuac!pa.dec.com!engage.pko.dec.com!e2big.mko.dec.com!jrdzzz.jrd.dec.com!jit533.jit.dec.com!diamond
  3. From: diamond@jit533.jit.dec.com (Norman Diamond)
  4. Subject: Re: typedef void (*generic)()
  5. Message-ID: <BzDqIs.1KI@jrd.dec.com>
  6. Sender: usenet@jrd.dec.com (USENET News System)
  7. Nntp-Posting-Host: jit533.jit.dec.com
  8. Reply-To: diamond@jit.dec.com (Norman Diamond)
  9. Organization: Digital Equipment Corporation Japan , Tokyo
  10. References: <1992Dec16.172748.24120@bernina.ethz.ch>
  11. Date: Thu, 17 Dec 1992 01:23:16 GMT
  12. Lines: 34
  13.  
  14. In article <1992Dec16.172748.24120@bernina.ethz.ch> hoesel@igc.ethz.ch (Frans van Hoesel) writes:
  15. >I have a problem in doing what I want to do in c.
  16. >#include <stdio.h>
  17. >#include <stdlib.h>
  18. >typedef void (*generic)();
  19. >main() {
  20. >        generic f,g;
  21. >        int     i;
  22. >        f = (generic) printf;
  23. >        g = (generic) abs;
  24. >    i=-4;
  25. >    /* and now do something like :printf("some text %d\n",abs(i)
  26. >      ), but using f and g instead !*/
  27. >}
  28. >This isn't the right group to ask (I know), but propably the best
  29. >There is also a remark from my compiler on some constructs stating that
  30. >void pointer cannot be cast to function pointers. Is this allowed?
  31.  
  32. Pointers to object or incomplete types cannot be converted to pointers
  33. to function types, nor vice-versa (except for the special case of
  34. null pointer constants being converted to pointers to function types).
  35.  
  36. However, any pointer to function type can be converted to any other
  37. pointer to function type.  You have to convert it back before calling
  38. though.  So in this sense, your generic type is indeed generic.
  39.  
  40. You can do this:
  41.     ((int (*) (char *, ...)) f) ( "some text %d\n",
  42.                                   ((int (*) (int)) g) (i)
  43.                                 );
  44. --
  45. Norman Diamond       diamond@jit081.enet.dec.com
  46. If this were the company's opinion, I wouldn't be allowed to post it.
  47. "It's been a lovely recession."
  48.