home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / c / 13478 < prev    next >
Encoding:
Text File  |  1992-09-10  |  1.3 KB  |  46 lines

  1. Path: sparky!uunet!gatech!prism!xray.gatech.edu!cc100aa
  2. From: cc100aa@xray.gatech.edu (Ray Spalding)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How to call procedure with a variable?!
  5. Keywords: C VARIABLE PROCEDURE
  6. Message-ID: <67971@hydra.gatech.EDU>
  7. Date: 10 Sep 92 19:36:37 GMT
  8. References: <1992Sep10.143705.15262@galileo.cc.rochester.edu>
  9. Sender: news@prism.gatech.EDU
  10. Organization: Georgia Institute of Technology
  11. Lines: 33
  12.  
  13. In article <1992Sep10.143705.15262@galileo.cc.rochester.edu> eric@prodigal.psych.rochester.edu (Eric Hansen) writes:
  14. >I'd like to be able to call a procedure based on the contents of a variable.
  15. >For instance, if my variable  GOTOPROC is equal to "GetNewSearch(X,Y,Z)", then
  16. >I'd like to somehow say GOTOPROC and have it perform the GetNewSearch
  17. >routine.
  18.  
  19. You can just declare a variable of type pointer to function, and
  20. assign (compatible) declared functions to it.  For example:
  21.  
  22. #include    <stdio.h>
  23.  
  24. int
  25. getnewsearch(int x, int y, int z)
  26. {
  27.     return x + y + z;
  28. }
  29.  
  30. int (*gotoproc)(int x, int y, int z);
  31.  
  32. int
  33. main()
  34. {
  35.     int t;
  36.  
  37.     gotoproc = getnewsearch;
  38.     t = gotoproc(1,2,3);
  39.     printf("%d\n",t);
  40.     return 0;
  41. }
  42. -- 
  43. Ray Spalding, Office of Information Technology
  44. Georgia Institute of Technology, Atlanta Georgia, 30332-0715
  45. Internet: ray.spalding@oit.gatech.edu (NeXT Mail accepted)
  46.