bouma@cs.purdue.EDU (William J. Bouma) (10/13/89)
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! ?????? Also, C doesn't care if I call the function: (*f[x])(); or (f[x])(); ie. both of these work. Why? Is the first one the "correct" way? -- Bill <bouma@cs.purdue.edu> || ...!purdue!bouma
chris@mimsy.UUCP (Chris Torek) (10/13/89)
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)(); % cdecl explain int (**f)() declare f as pointer to pointer to function returning int So far, so good: f can point to the first of `n' pointers to functions returning int. >And then I tried mallocing some space for it like this: > f = (int (**)()) malloc(n * sizof(*f)); Since you misspelled `sizeof' above, I have to conclude that this is not extracted directly from the source that gave you the error. Hence: >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! Were sizeof spelled correctly above, that line would be fine (provided that you have declared malloc() at some earlier point, but even otherwise, pcc would not complain). I must conclude that either your compiler is broken, or the text quoted above (with sizeof corrected) is not the text you fed to that compiler. >Also, C doesn't care if I call the function: > (*f[x])(); >or > (f[x])(); >ie. both of these work. Why? Is the first one the "correct" way? Both will work in any (proposed) ANSI C compiler, but the former is required by many older compilers. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@cs.umd.edu Path: uunet!mimsy!chris
kremer@cs.odu.edu (Lloyd Kremer) (10/13/89)
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!
cpcahil@virtech.UUCP (Conor P. Cahill) (10/13/89)
In article <8247@medusa.cs.purdue.edu>, bouma@cs.purdue.EDU (William J. Bouma) writes: > int (**f)(); > > And then I tried mallocing some space for it like this: > > f = (int (**)()) malloc(n * sizof(*f)); ^^^^^ sizeof( This worked fine (without any compiler complaints) on both gcc and pcc under System V/386 Rel 3.2. > ?????? Also, C doesn't care if I call the function: > (*f[x])(); or (f[x])(); The first one is conceptually correct, but most compilers will allow both of them. I *think* ANSI requires that both be accepted. -- +-----------------------------------------------------------------------+ | Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 ! | Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 | +-----------------------------------------------------------------------+