Calling functions by address

David Lai lai at vedge.UUCP
Thu Sep 1 08:01:20 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 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.

You're missing the function call parenthesis: (*addressarray[code])();

the example below works, and is approx what you want to do
(besides out news wont let me send the above unless I have more
 new text):

typedef int (*ptr_to_int_returning_func)(); /* declares a type for readability */
extern int printf(), fprintf(), scanf(), strlen();

ptr_to_int_returning_func array_of_func_ptrs[]=
	{
	printf,
	fprintf,
	scanf,
	strlen
	};	/* get the idea? */

main()
{
/* lets do a few calls */
int i;
i = (*array_of_func_ptrs[3])("test_strlen");
(*array_of_func_ptrs[0])("size was %d\n",i); /* test printf */
}

I hope the example helps with the nasty syntax.
-- 
	"What is a DJ if he can't scratch?"  - Uncle Jamms Army
The views expressed are those of the author, and not of Visual Edge, nor Usenet.
David Lai (vedge!lai at larry.mcrcim.mcgill.edu || ...watmath!onfcanim!vedge!lai)
-- 
	"What is a DJ if he can't scratch?"  - Uncle Jamms Army
The views expressed are those of the author, and not of Visual Edge, nor Usenet.
David Lai (vedge!lai at larry.mcrcim.mcgill.edu || ...watmath!onfcanim!vedge!lai)



More information about the Comp.std.c mailing list