home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16214 < prev    next >
Encoding:
Text File  |  1992-11-09  |  2.1 KB  |  65 lines

  1. Path: sparky!uunet!crdgw1!newsun!news
  2. From: damurphy@wc.novell.com (Duane Murphy)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: need to create a jump table
  5. Message-ID: <1992Nov9.160458.1660@novell.com>
  6. Date: 9 Nov 92 16:04:58 GMT
  7. References: <1992Nov3.025210.27336@ryn.mro4.dec.com>
  8. Sender: news@novell.com (The Netnews Manager)
  9. Organization: Novell, Inc.
  10. Lines: 50
  11. X-Xxdate: Mon, 9 Nov 92 16:08:48 GMT
  12. Nntp-Posting-Host: 130.57.72.123
  13. X-Useragent: Nuntius v1.1.1d12
  14.  
  15. In article Alice Uyeno, writes:
  16. >I'm trying to develop a jump table.  Actually, the scenario is that I
  17. have two 
  18. >jump tables.  Depending on the value of a certain flag, I use one or the
  19. other. 
  20. > Each jump table consists of pointers to functions.  I'm wondering if it
  21. is 
  22. >possible to have the functions with varying number of parameters.  For
  23. example, 
  24. >if my jump table contains pointers to ErrorRoutine and ValidateCmd,
  25. where the 
  26. >prototypes for the routines are as follows:
  27. >
  28. >    void ErrorRoutine(int *ErrCode, struct foo * MyPtr);
  29. >        void ValidateCmd(struct foo * CmdPtr);
  30. >
  31. >void (* FcnTbl[])() = {ErrorRoutine,ValidateCmd};
  32. >
  33. >where 
  34. >    ptr = FcnTbl;
  35. >
  36. >and ptr is how the jump table is referenced.
  37.  
  38.  
  39. Sometimes when you think you are developing a jump table you are actually 
  40. developing a jump structure or rather a structure that contains pointers 
  41. to functions.  
  42.  
  43. If all of the functions in the table take (and return) the same type and 
  44. number of arguments then indeed you have a function table.  If, on the 
  45. other hand, you have functions that have different numbers or types of 
  46. parameters or return values then you could define a function structure 
  47. instead.
  48.  
  49. The significant advantage to using a function structure in this case is 
  50. that you will not have to cast function pointers to persuade the compiler 
  51. to do what you think you ought to be doing.  Let the compiler help you 
  52. and help to enforce the prototypes for the functions in the structure.  
  53. In the long run you will benifit greatly.
  54.  
  55. Example (untested!):
  56.  
  57. struct {
  58.     void (*error)(int *, struct foo *);
  59.     void (*validate)(struct foo*);
  60. } FcnTbl = {ErrorRoutine, ValidateCmd};
  61.  
  62. I hope this helps.
  63.  
  64. ...Duane
  65.