Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!gem.mps.ohio-state.edu!uakari.primate.wisc.edu!xanth!kremer From: kremer@cs.odu.edu (Lloyd Kremer) Newsgroups: comp.lang.c Subject: Re: pointers to pointers to functions Summary: The compiler is not too smart. Message-ID: <10174@xanth.cs.odu.edu> Date: 12 Oct 89 18:13:11 GMT References: <8247@medusa.cs.purdue.edu> Distribution: usa Organization: Old Dominion University, Norfolk, Va. Lines: 61 In article <8247@medusa.cs.purdue.edu> bouma@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!