home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!crdgw1!newsun!news
- From: damurphy@wc.novell.com (Duane Murphy)
- Newsgroups: comp.lang.c
- Subject: Re: need to create a jump table
- Message-ID: <1992Nov9.160458.1660@novell.com>
- Date: 9 Nov 92 16:04:58 GMT
- References: <1992Nov3.025210.27336@ryn.mro4.dec.com>
- Sender: news@novell.com (The Netnews Manager)
- Organization: Novell, Inc.
- Lines: 50
- X-Xxdate: Mon, 9 Nov 92 16:08:48 GMT
- Nntp-Posting-Host: 130.57.72.123
- X-Useragent: Nuntius v1.1.1d12
-
- In article Alice Uyeno, writes:
- >I'm trying to develop a jump table. Actually, the scenario is that I
- have two
- >jump tables. Depending on the value of a certain flag, I use one or the
- other.
- > Each jump table consists of pointers to functions. I'm wondering if it
- is
- >possible to have the functions with varying number of parameters. For
- example,
- >if my jump table contains pointers to ErrorRoutine and ValidateCmd,
- where the
- >prototypes for the routines are as follows:
- >
- > void ErrorRoutine(int *ErrCode, struct foo * MyPtr);
- > void ValidateCmd(struct foo * CmdPtr);
- >
- >void (* FcnTbl[])() = {ErrorRoutine,ValidateCmd};
- >
- >where
- > ptr = FcnTbl;
- >
- >and ptr is how the jump table is referenced.
-
-
- Sometimes when you think you are developing a jump table you are actually
- developing a jump structure or rather a structure that contains pointers
- to functions.
-
- If all of the functions in the table take (and return) the same type and
- number of arguments then indeed you have a function table. If, on the
- other hand, you have functions that have different numbers or types of
- parameters or return values then you could define a function structure
- instead.
-
- The significant advantage to using a function structure in this case is
- that you will not have to cast function pointers to persuade the compiler
- to do what you think you ought to be doing. Let the compiler help you
- and help to enforce the prototypes for the functions in the structure.
- In the long run you will benifit greatly.
-
- Example (untested!):
-
- struct {
- void (*error)(int *, struct foo *);
- void (*validate)(struct foo*);
- } FcnTbl = {ErrorRoutine, ValidateCmd};
-
- I hope this helps.
-
- ...Duane
-