pointers to functions

latham at bsdpkh.UUCP latham at bsdpkh.UUCP
Tue Jun 24 17:11:07 AEST 1986


In article <237 at killer.UUCP> toma at killer.UUCP (Tom Armistead) writes:
>The delima - Is it possible to cast a 'pointer to' a function?
>
>What I have is a char * that holds the address of a function.
>int	foo();
>char	*addr = foo;
>Would it be possible to call the function foo() via the variable addr?
>
>Secondly is it possible to have an array of type 'pointer to' function.
>i.e. 
>int  (*function)()[SIZE];
>something like the above, I was able to achieve this using this:
... etc
>Thanx in advance for **ANY** help,
>
>Tom Armistead

FIRST, char *addr = foo;  is a BAAADDDD idea! and NO you can't call foo with it
   ( well you can but .... )

Try, 
	int (* addr)() = foo;

or in general :

	int (* funcptr)();

SECOND an array of such pointers is declared :

	int (* funcptr[])();

	(* funcptr[])  being an array of pointers;
	(* funcptr[])()  ... to functions;
	int (* funcptr[])(); returning an integer;

A BETTER IDEA is to declare 'funcptr' as a type

	typedef int (* funcptr)();

then use it ...

	funcptr foo[1028];

This is much clearer.

incidentally,    ( (int *())addr)();  would do it     (I think?)


			Ken Latham, AT&T-IS (via AGS Inc.), Orlando , FL

			uucp: ihnp4!bsdpkh!latham



More information about the Comp.lang.c mailing list