home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16826 < prev    next >
Encoding:
Text File  |  1992-11-19  |  1.7 KB  |  52 lines

  1. Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!ames!network.ucsd.edu!sdcc12!sdcc10!cs161feu
  2. From: cs161feu@sdcc10.ucsd.edu (Anthony Minkoff)
  3. Newsgroups: comp.lang.c
  4. Subject: Array of pointers to constant functions?
  5. Message-ID: <41289@sdcc12.ucsd.edu>
  6. Date: 19 Nov 92 22:19:45 GMT
  7. Sender: news@sdcc12.ucsd.edu
  8. Organization: University of California, San Diego
  9. Lines: 40
  10. Nntp-Posting-Host: sdcc10.ucsd.edu
  11.  
  12.  
  13. I want to declare an array of pointers to constant functions.  (Is
  14. that last bit redundant?)  I.e., what I want is an array of pointers
  15. to functions.  Each of these functions is defined at compile time,
  16. and I'd rather not have to give a name to each of these functions
  17. (except, of course, for the name of the array).
  18.  
  19. To make it a little clearer, the following code fragment describes
  20. the essence of what I want to do (except that the below code is not,
  21. of course, syntactically correct C):
  22.  
  23.  
  24.         main()
  25.         {
  26.             void (*functions) [2] ( int x ) = {
  27.                 { printf("zero! %d\n", x); } ,
  28.                 { printf("one! %d\n", x); }
  29.                                               } ;
  30.  
  31.             int n;
  32.  
  33.             /* somehow choose n = 0 or n = 1 */
  34.  
  35.             functions[n]();
  36.         }
  37.  
  38.  
  39. Does C support what I want to do here?  If so, how do I do it?  I
  40. have not been able to find it in reference books.  (The functions,
  41. of course, could be any arbitrary functions.  I realize there are
  42. much easier ways of doing what this code would do, but I'm
  43. interested in the general problem.  This example is used merely for
  44. illustration.)
  45.  
  46. Thank you very much.
  47.  
  48. Tony 
  49.  
  50. P.S.: If there is no solution in C, but there is one in C++, I am
  51. also interested in that.  However, I would prefer a C solution.
  52.