Calling functions by address

Robert Viduya byron at pyr.gatech.EDU
Wed Aug 31 03:38:24 AEST 1988


In article <679 at mssx.UUCP> src at 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 at pyr.gatech.edu  uucp:	...!gatech!pyr!byron



More information about the Comp.lang.c mailing list