pointers to pointers to functions
Lloyd Kremer
kremer at cs.odu.edu
Fri Oct 13 04:13:11 AEST 1989
In article <8247 at medusa.cs.purdue.edu> bouma at cs.purdue.EDU (William J. Bouma) writes:
> I need a list of pointers to functions to be malloced. What is
> the syntax? I declared this thing to hold it:
>
> int (**f)();
>
> And then I tried mallocing some space for it like this:
>
> f = (int (**)()) malloc(n * sizof(*f));
>
> When the compiler hits that line I get this:
>
> illegal lhs of assignment operator
> unacceptable operand of &
> warning: illegal pointer/integer combination, op =
> cannot recover from earlier errors: goodbye!
There is nothing wrong with this code (other than the misspelling of sizeof).
Some compilers have trouble with complicated double indirection. They're
broken; what can I say? Sometimes they need a little help in the form of
a simplifying typedef, such as:
typedef int (*PFI)(); /* Pointer to Function returning Int */
PFI *f; /* pointer to a PFI */
f = (PFI *)malloc(n * sizeof(*f));
Some programmers are helped by this nomenclature also. :-)
> Also, C doesn't care if I call the function:
>
> (*f[x])();
>
> or
>
> (f[x])();
Right, it doesn't. I prefer the first form since it reminds the reader
that programmer-defined function pointers are in use. One must remember
that a function name without its argument list is taken as a pointer to
the function. Hence a simple function call like
printf("Hello, world\n");
is, syntactically, a pointer to a function followed by a parenthesized
argument list. It is syntactically equivalent to
(*printf)("Hello, world\n");
and should compile to exactly the same thing.
--
Lloyd Kremer
...!uunet!xanth!kremer
Have terminal...will hack!
More information about the Comp.lang.c
mailing list