An array of function pointers.
Nathaniel Ingersoll
nate at altos86.UUCP
Wed Sep 21 08:11:22 AEST 1988
In article <414 at ucrmath.UUCP> jantypas at 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 at sun.com
More information about the Comp.lang.c
mailing list