pointers to functions
Chris Torek
chris at umcp-cs.UUCP
Sun Jun 22 11:06:01 AEST 1986
[I could swear this has been answered before, but here goes:]
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?
Possible: yes; portable: no.
>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?
Technically, that should be `char *addr = (char *) foo;'. If this happens
not to lose important information, you can subsequently successfully use
value = (*(int (*)())addr)(arg1, arg2, ...);
The cast `(int (*)())', odd-looking though it may be, coerces the type
to `pointer to function returning int'.
>Secondly is it possible to have an array of type 'pointer to' function.
>i.e.
>int (*function)()[SIZE];
Certainly it is possible; you just need to construct the proper
declaration. For example, feeding the description
declare f as array 10 of pointer to function returning int
through cdecl produces
int (*f[10])()
There are at least two programs that do what cdecl does; as I recall,
the one I do not have was posted less than three months ago to either
net.sources or mod.sources. Cdecl itself was posted to net.sources
over a year ago.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP: seismo!umcp-cs!chris
CSNet: chris at umcp-cs ARPA: chris at mimsy.umd.edu
More information about the Comp.lang.c
mailing list