Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!cwjcc!gatech!gitpyr!byron From: byron@pyr.gatech.EDU (Robert Viduya) Newsgroups: comp.lang.c Subject: Re: Calling functions by address Keywords: functions calling Message-ID: <6351@pyr.gatech.EDU> Date: 30 Aug 88 17:38:24 GMT References: <679@mssx.UUCP> Reply-To: byron@pyr.UUCP (Byron A Jeff) Organization: Georgia Institute of Technology Lines: 70 In article <679@mssx.UUCP> src@mssx.UUCP (Pleschutznig Andreas) writes: >Suppose following: > >We want to write a software emulation for another processor on UNIX > >So the main problem is to get into the emulation routines as fast as possible, >and therefore it doe not seem to be good enough to do > > switch (code) { > case .. > case .. > } > >So we thought of doing that job by declaring the addresses of the emulation >routines and jumping to the routines by address like this > > (*addressarray[code]); > >I know, I know that *does not* work, but maybe there is someone knowing to >get around. Here's something I came up with in 5 minutes that seems to do the job. The declaration for variable functions is an array of pointers to functions that return integer. Note how the '*' and the '[]' must be put in parens so they are evaluated first. ------ Cut Here ------ int one(), two(), three(), four(), five(); int (*functions[])() = {one,two,three,four,five}; main(argc,argv) { if (argc > 0 && argc < 6) functions[argc-1](); else printf("Invalid number of arguments\n"); } one() { printf("There is one argument on the command line\n"); } two() { printf("There are two arguments on the command line\n"); } three() { printf("There are three arguments on the command line\n"); } four() { printf("There are four arguments on the command line\n"); } five() { printf("There are five arguments on the command line\n"); } -- Another random extraction from the mental bit stream of... Byron A. Jeff Georgia Tech, Atlanta GA 30332 Internet: byron@pyr.gatech.edu uucp: ...!gatech!pyr!byron