home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / std / c / 3243 < prev    next >
Encoding:
Internet Message Format  |  1992-12-21  |  1.8 KB

  1. Xref: sparky comp.std.c:3243 comp.lang.c:18726
  2. Path: sparky!uunet!gossip.pyramid.com!olivea!charnel!sifon!thunder.mcrcim.mcgill.edu!mouse
  3. From: mouse@thunder.mcrcim.mcgill.edu (der Mouse)
  4. Newsgroups: comp.std.c,comp.lang.c
  5. Subject: Re: typedef void (*generic)()
  6. Message-ID: <1992Dec21.001537.12419@thunder.mcrcim.mcgill.edu>
  7. Date: 21 Dec 92 00:15:37 GMT
  8. References: <1992Dec16.172748.24120@bernina.ethz.ch>
  9. Followup-To: comp.lang.c
  10. Organization: McGill Research Centre for Intelligent Machines
  11. Lines: 39
  12.  
  13. In article <1992Dec16.172748.24120@bernina.ethz.ch>, hoesel@igc.ethz.ch (Frans van Hoesel) writes:
  14.  
  15. > I have a problem in doing what I want to do in c.
  16.  
  17. > typedef void (*generic)();
  18.  
  19. >         f = (generic) printf;
  20. >         g = (generic) abs;
  21.  
  22. >     /* and now do something like :printf("some text %d\n",abs(i)
  23. >       ), but using f and g instead !*/
  24.  
  25. > This isn't the right group to ask (I know), but propably the best
  26.  
  27. Asking elsewhere than in the right group is never best.  I'm taking
  28. steps to move it to comp.lang.c, since it really is not a standard
  29. question, but a generic C question.
  30.  
  31. As for how you'd do it,
  32.  
  33. (*(int (*)(const char *, ...))f)("some text %d\n",(*(int (*)(int))g)(i));
  34.  
  35. Note the casts on the functions!  The compiler may be using different
  36. calling sequences depending on the argument pattern and return type, so
  37. for the calls to work you must cast the pointers correctly.  (The two
  38. "(*(" sequences may be replaced by "((" instead; I just happen to like
  39. to follow my function pointers before calling them.)
  40.  
  41. > There is also a remark from my compiler on some constructs stating
  42. > that void pointer cannot be cast to function pointers.  Is this
  43. > allowed?
  44.  
  45. Only in the special case of a null pointer constant.  Object pointers
  46. and function pointers are not, in general, interconvertible.
  47.  
  48.                     der Mouse
  49.  
  50.                 mouse@larry.mcrcim.mcgill.edu
  51.