From: jfbode AT nospam DOT mail DOT earthlink DOT net (John Bode) Newsgroups: comp.lang.c,comp.os.msdos.djgpp Subject: Re: Menus and mouse: how to tell what to do? Date: Mon, 27 Jul 1998 08:16:59 -0500 Organization: EarthLink Network, Inc. Lines: 91 Message-ID: References: <35ba510b DOT 361228 AT news DOT unisys DOT com DOT br> NNTP-Posting-Host: 1cust201.tnt16.hou3.da.uu.net Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk In article <35ba510b DOT 361228 AT news DOT unisys DOT com DOT br>, brunobg AT geocities DOT com (Bruno Barberi Gnecco) wrote: > I'd like to know if it's possible to create and array of function names. For > instance, let's say that a menu has 5 options; if I could just detect what is the > chosen option (let's say, n), I could use the stored function name at the position > n, instead of a long switch. Or maybe, a more clever approach to solve this menu It is possible to create an array of function pointers. If you want to execute a function based on its name, you'll have to create some kind of a lookup table. Here's a quick (untested!) example: #define MAX_FUNC ... /* max number of functions in table */ /* ** Typedef for function pointer. Since you don't mention what the functions ** take as parameters or what they return, we'll assume functions taking an ** int and a double argument and returning int. If you're going to have an ** array of function pointers, all the functions need to have the same ** prototype. */ typedef int (*fptr_t) (int, double); /* ** Struct definition for lookup table */ struct flook { char fname[33]; /* function name (up to 32 chars long) */ fptr_t fptr; /* pointer to function */ }; /* ** Define functions here. Assume they are named f1, f2, f3, etc. */ ... int main (void) { /* ** Create and initialize lookup table */ struct flook ftable[MAX_FUNC] = {{"f1", f1}, {"f2", f2}, {"f3", f3}, ...}; int i; int x; double y; char fname[33]; int result; ... /* ** Search the lookup table based on name, and execute the indicated function ** if found */ for (i = 0; i < MAX_FUNC; i++) { if (!strcmp (fname, flook[i].fname)) { result = (*(flook[i].fptr)) (x, y); break; } } } > problem... Actually, I have the same problem with the mouse. > What's the best way to tell the program: 'If you click here, do it', or 'If you > choose this option, do that'??? To handle the mouse hot-spots, I thought about > creating a struct like: > struct mousespot { > char minx, maxx, miny, maxy; > ?function? // problem here > } > and check: if ( cursor < maxx && cursor > minx && cursor < maxy && cursor > miny > ), but how to handle the function? Would a pointer to a function work? How to > declare it? > > "There's never enough time to do all the nothing you want" Bill Watterson > Bruno Barberi Gnecco ICQ #1383173 - PGP 5.0i user > My other OS is Linux -=- http://graphx.home.ml.org -=- Electric Eng, Poli -- John Bode one grumpy code monkey A Programmer writes code that will run at the end of the day. A Software Engineer writes code that will run ten years from now. To email me directly, remove the 'nospam.' from my address.