joshi@motcid.UUCP (Abhay B. Joshi) (02/13/91)
I would like to declare a function which returns a pointer to a function (that returns an integer). I have been unable to strike at the correct syntax of such a declaration. I tried the following (among others): ((int *)()) func() ; Doesn't work. Thanks for any hints/answers. -- Abhay --
dwebster@cs.arizona.edu (Dave E. Webster) (02/13/91)
In article <5806@agate.UUCP> joshi@motcid.UUCP (Abhay B. Joshi) writes: >I would like to declare a function which returns a pointer to a function >(that returns an integer). I have been unable to strike at the correct >syntax of such a declaration. > >I tried the following (among others): > ((int *)()) func() ; > >Doesn't work. Abhay: You might try the following: int (*func(int,int)) (float) /* This assumes ANSI C prototypes */ { ... } This defines "func" as a "function with two integer parameters which returns a pointer to a function with a single float parameter which returns an integer." You will need to modify the parameters to suit your particular needs, of course. Note that the parenthesis around the main function prototype are not optional since the `()' have a higher binding precedence than the `*'. This is where c becomes extremely powerful (read EXTREMELY DANGEROUS). Dave 8-}. Newsgroups: comp.lang.c Subject: Re: A question on Function declaration Summary: Expires: References: <5806@agate.UUCP> Sender: Followup-To: Distribution: Organization: U of Arizona CS Dept, Tucson Keywords: pointer function integer
sland@motcid.UUCP (Stephen Shortland) (02/13/91)
joshi@motcid.UUCP (Abhay B. Joshi) writes: >I would like to declare a function which returns a pointer to a function >(that returns an integer). I have been unable to strike at the correct >syntax of such a declaration. >I tried the following (among others): > ((int *)()) func() ; >Doesn't work. >Thanks for any hints/answers. Have you tried: int (*func())() I think that this should do what you want. -- | Stephen Shortland, | Motorola Ireland Ltd, | | | | Mahon Industrial Estate, | | | Phone +353-21-357101 | Blackrock, Cork, | ..uunet!motcid!sland | | Fax. +353-21-357635 | IRELAND. | |
wittig@gmdzi.gmd.de (Georg Wittig) (02/13/91)
joshi@motcid.UUCP (Abhay B. Joshi) writes: >I would like to declare a function which returns a pointer to a function >(that returns an integer). Script started on Wed Feb 13 15:28:05 1991 % cdecl declare x as pointer to function returning pointer to function returning int int (*(*x)())() ^D% script done on Wed Feb 13 15:28:39 1991 -- Georg Wittig GMD-Z1.IT P.O.Box 1240 | "Freedom's just another word D-W-5205 St. Augustin 1 (Germany) | for nothing left to lose" email: wittig@gmdzi.gmd.de | (from "Me and Bobby McGee", telephone: (+49) 2241 14-2294 | Janis Joplin, Kris Kristofferson)
newberry@nmsu.edu (Jeff Newberry) (02/14/91)
>I would like to declare a function which returns a pointer to a function >(that returns an integer). I have been unable to strike at the correct >syntax of such a declaration. > >I tried the following (among others): > ((int *)()) func() ; > >Doesn't work. > >Thanks for any hints/answers. I believe the following should work... typedef int (*PFI)(); /* type PFI = pointer to a function that returns int */ PFI func(); { ... } Jeff Newberry
brianh@hpcvia.CV.HP.COM (brian_helterline) (02/14/91)
Abhay B. Joshi writes: >I would like to declare a function which returns a pointer to a function >(that returns an integer). I have been unable to strike at the correct >syntax of such a declaration. >I tried the following (among others): > ((int *)()) func() ; >Doesn't work. You need parenthesis around the '*' like this: (int (*)()) func(); ------------------------------------------------------------------------------- Brian Helterline | | brianh@hpcvia.cv.HP.COM Hewlett Packard | empty space here | ..!hp-pcd!hpcvia!brianh Corvallis, OR 97330 | | -------------------------------------------------------------------------------
browns@iccgcc.decnet.ab.com (Stan Brown) (02/14/91)
In article <5807@marble.UUCP>, sland@motcid.UUCP (Stephen Shortland) writes: > joshi@motcid.UUCP (Abhay B. Joshi) writes: > >>I would like to declare a function which returns a pointer to a function >>(that returns an integer). I have been unable to strike at the correct >>syntax of such a declaration. > >>I tried the following (among others): >> ((int *)()) func() ; >>Doesn't work. I'm just a wussy little fraidy-cat (TM), so I do it in stages. You don't say what arguments either of the functions takes, so I'll assume one takes a char * and the other takes a double. A function that takes a char * and returns an int is 'int func(char *);'. Its type might be called 'FC_I': typedef int FC_I(char *); A pointer to such a function would be a 'FC_I *'. So a function that takes a double and returns such a pointer would be FC_I *desired_func(double); This goes in two steps, unlike the other solutions I've seen. But it's imposible to get the parentheses wrong with this method. Comments from anyone? Disadvantages I've missed? Hey--this is all my opinion, nobody else's. Rely on it at your peril. email: browns@iccgcc.decnet.ab.com Stan Brown, Oak Road Systems, Cleveland, Ohio, USA +1 216 371 0043
mcdaniel@adi.com (Tim McDaniel) (02/14/91)
The program cdecl is helpful in such cases, although I think it's better to work with a precedence chart to figure out what it should be. declare f as function (int, int) returning pointer to function (float) returning int yields int (*f(int , int ))(float ) and declare f as function returning pointer to function returning int yields int (*f())() You can use one or the other depending on whether you have prototypes available or not. It has been posted on the net; e-mail me for a copy if you have to, but please check a comp.sources.unix and comp.sources.misc archive site first. -- "I'd hang you from my nipples ... but it would shock the children." Tim McDaniel Applied Dynamics Int'l.; Ann Arbor, Michigan, USA Internet: mcdaniel@adi.com UUCP: {uunet,sharkey}!amara!mcdaniel
bliss@sp64.csrd.uiuc.edu (Brian Bliss) (02/14/91)
> I would like to declare a function which returns a pointer to a function >(that returns an integer). int (*f( ))(); ^arg list goes here (arg list for returned func goes in trailing parens) >I tried the following (among others): > ((int *)()) func() ; it is never legal to put parenthesis around a type specifier, except when performing the cast operation. just remember that declaration look like the var's usage, so you have a function f: f(); a function returning a pointer: * f(); or *(f()); "apply the function, then dereference the ptr" a function returning a pointer to a function (*f())(); or (*(f()))(); "apply the function, deref ptr, apply result" bb
scs@adam.mit.edu (Steve Summit) (02/14/91)
The people-who-ask-questions are doing a pretty good job of reading the Frequently Asked Questions list and not asking questions on it. The people-who-know-enough-to-answer-questions need to be a little more careful about contributing to the secondary rash of Frequently Posted Answers. If you know enough that the FAQ list probably won't teach you anything, skim through it once in a while anyway, so you'll know what questions you don't have to waste your time answering (and, consequently, the rest of us shouldn't have to keep reading answers to). If a question is on the FAQ list, you can rest assured that anyone who happens to ask it receives ample mailed answers, without any necessity for posted followups. (I and several other people summarily mail copies of the entire FAQ list in response to verbatim questions.) Sorry for this interruption; we now return you to your regularly- scheduled microoptimization workshop. (I shouldn't sound snide; the current one is actually meaningful, and hasn't used *any* bitwise shift operators yet.) Steve Summit scs@adam.mit.edu
rmartin@clear.com (Bob Martin) (02/15/91)
In article <5806@agate.UUCP> joshi@motcid.UUCP (Abhay B. Joshi) writes: >I would like to declare a function which returns a pointer to a function >(that returns an integer). I have been unable to strike at the correct >syntax of such a declaration. > >I tried the following (among others): > ((int *)()) func() ; > >Doesn't work. > >Thanks for any hints/answers. > >-- Abhay -- Try this one. Net people please check!!! int (*p)() p is a pointer to a function returning int. (int (*)()) f() f is a func returning a pointer to a func returning int. You are much better off building your type via typedefs!! typedef int (*pfint)(); /* ptr to function returning int */ pfint f(); /* declares f as func returning ptr to func returning int */ -- +-Robert C. Martin-----+:RRR:::CCC:M:::::M:| Nobody is responsible for | | rmartin@clear.com |:R::R:C::::M:M:M:M:| my words but me. I want | | uunet!clrcom!rmartin |:RRR::C::::M::M::M:| all the credit, and all | +----------------------+:R::R::CCC:M:::::M:| the blame. So there. |