home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!ames!network.ucsd.edu!sdcc12!sdcc10!cs161feu
- From: cs161feu@sdcc10.ucsd.edu (Anthony Minkoff)
- Newsgroups: comp.lang.c
- Subject: Array of pointers to constant functions?
- Message-ID: <41289@sdcc12.ucsd.edu>
- Date: 19 Nov 92 22:19:45 GMT
- Sender: news@sdcc12.ucsd.edu
- Organization: University of California, San Diego
- Lines: 40
- Nntp-Posting-Host: sdcc10.ucsd.edu
-
-
- I want to declare an array of pointers to constant functions. (Is
- that last bit redundant?) I.e., what I want is an array of pointers
- to functions. Each of these functions is defined at compile time,
- and I'd rather not have to give a name to each of these functions
- (except, of course, for the name of the array).
-
- To make it a little clearer, the following code fragment describes
- the essence of what I want to do (except that the below code is not,
- of course, syntactically correct C):
-
-
- main()
- {
- void (*functions) [2] ( int x ) = {
- { printf("zero! %d\n", x); } ,
- { printf("one! %d\n", x); }
- } ;
-
- int n;
-
- /* somehow choose n = 0 or n = 1 */
-
- functions[n]();
- }
-
-
- Does C support what I want to do here? If so, how do I do it? I
- have not been able to find it in reference books. (The functions,
- of course, could be any arbitrary functions. I realize there are
- much easier ways of doing what this code would do, but I'm
- interested in the general problem. This example is used merely for
- illustration.)
-
- Thank you very much.
-
- Tony
-
- P.S.: If there is no solution in C, but there is one in C++, I am
- also interested in that. However, I would prefer a C solution.
-