Path: utzoo!attcan!uunet!altnet!altos86!nate From: nate@altos86.UUCP (Nathaniel Ingersoll) Newsgroups: comp.lang.c Subject: Re: An array of function pointers. Message-ID: <526@altos86.UUCP> Date: 20 Sep 88 22:11:22 GMT References: <414@ucrmath.UUCP> Reply-To: nate@altos86.UUCP (Nathaniel Ingersoll) Organization: Altos Computer Systems, San Jose, CA Lines: 69 In article <414@ucrmath.UUCP> jantypas@ucrmath.UUCP (John Antypas) writes: >drawing lines, circles etc. We'd like to set up something like a vector table. >If the vector table has a NULL in a spot, then the interpreter will use its >internal routines instead of faster device specific code. As an example: > >vectors[] = { dev_line(), > dev_box(), > dev_circle(), > dev_arc(), > dev_text() > } > >Thanks. > >John Antypas -- Soft21 --21st Century Software: I often have programs where I look up a function based on something like user input, and what I usually do is similar to: /* hjkl character movement ... */ int f_left(), f_right(), f_up(), f_down(); int (*vectors[0200])(); init_vec() { vectors['h'] = f_left; vectors['j'] = f_down; vectors['k'] = f_up; vectors['l'] = f_right; } or, similarly, #define CASE1 0 #define CASE2 1 ... #define CASEN N /* some N */ where some input function returns a CASEx, and then you can have an array of function pointers like int (*vectors[how many])() = { f_case1, f_case2, ... }; and go like int whatcase, getcase(); int (*f)(), invalidcase(); whatcase = getcase(); if (whatcase >= 0 && whatcase < MAXCASE) f = vectors[whatcase]; else f = invalidcase; (*f)(...); Now that I've beaten this to death, good luck. -- Nathaniel Ingersoll Altos Computer Systems, SJ CA ...!ucbvax!sun!altos86!nate altos86!nate@sun.com