Pointer to function returning pointer to function returning... 
Author Message
 Pointer to function returning pointer to function returning...

I have a problem with the declaration of pointer to function.
In my program there are several functions that represent the steps in
a process (its a data-acquisition prject) and I would like to define
these to return a pointer to another of these functions so that the
main processing loop looks like

        P = P();

where P is a pointer to one of these functions. To fleshen this out:

        (*P)();         /* declaration of P as a pointer to function */
        (*Func1())();   /* Func1 declared as returning a pointer to func. */
        (*Func2())();   /* ditto */
        ...
        P = Func1;      /* initialisation of P */
        ...
        /* Main Loop */
        P = P();        /* function gets called and returns a new pointer */
                        /* to be stored in P */

        Func1() {
                ...
                return Func2;
        }

I hope you get what I mean. The Problem: HOW DO I DECLARE THOSE FUNCTIONS
CORRECTLY?! In my opinion I'm stuck in an infinite loop, if I want to
declare it in full. (P is a pointer to a function that returns a pointer to
a function that returns a pointer to a function that... ad infinitum)
Is there a solution to this? Do I have to use void pointers for this and
cast them when I want to call the functions or what?

Any Comments Would Really Be Appreciated!

Thanx,
        Roland


                Sorry, no sig. file yet.



Mon, 10 Oct 1994 03:51:16 GMT  
 Pointer to function returning pointer to function returning...

Quote:
>    (*P)();         /* declaration of P as a pointer to function */
>    (*Func1())();   /* Func1 declared as returning a pointer to func. */
>    (*Func2())();   /* ditto */
>    ...
>    P = Func1;      /* initialisation of P */
>    ...
>    /* Main Loop */
>    P = P();        /* function gets called and returns a new pointer */
>                    /* to be stored in P */

Maybe I'm missing something, but where exactly is the loop you are talking
about?  After your `loop' line, P will have whatever Func1 returns in it.

Chris



Mon, 10 Oct 1994 23:37:07 GMT  
 Pointer to function returning pointer to function returning...

Quote:

>I have a problem with the declaration of pointer to function.
>In my program there are several functions that represent the steps in
>a process (its a data-acquisition prject) and I would like to define
>these to return a pointer to another of these functions so that the
>main processing loop looks like

>    P = P();

>where P is a pointer to one of these functions. To fleshen this out:

>    (*P)();         /* declaration of P as a pointer to function */
>    (*Func1())();   /* Func1 declared as returning a pointer to func. */
>    (*Func2())();   /* ditto */
>    ...
>    P = Func1;      /* initialisation of P */
>    ...
>    /* Main Loop */
>    P = P();        /* function gets called and returns a new pointer */
>                    /* to be stored in P */

try the following line in place of the above call:
        P = (*P)();      /* P is a POINTER that must be dereferenced here */

Quote:

>    Func1() {

Define Func1 here as in the declaration above.

Quote:
>            ...
>            return Func2;
>    }

>I hope you get what I mean. The Problem: HOW DO I DECLARE THOSE FUNCTIONS
>CORRECTLY?! In my opinion I'm stuck in an infinite loop, if I want to
>declare it in full. (P is a pointer to a function that returns a pointer to
>a function that returns a pointer to a function that... ad infinitum)
>Is there a solution to this? Do I have to use void pointers for this and
>cast them when I want to call the functions or what?

>Any Comments Would Really Be Appreciated!

>Thanx,
>    Roland

Your declarations are correct, at the top of the file.  You do'nt need
a recursive pointer to ... in your definitions, but you DO need to
define things correctly, such that the declarations are consistent.

tim
--
________________ Tim Frost, Technical Consultant, GCS Ltd  __________________
_________ P.O. Box 11-642, Manners Street, Wellington, New Zealand. _________
______________ Voice: +64 4 801-8000   Fax: +64 4 801-8888 __________________



Tue, 11 Oct 1994 15:27:52 GMT  
 Pointer to function returning pointer to function returning...

Quote:

>[How do I declare a function such that this is legal?]
>    P = P();

Ah, the recursive-function-type question.  (Not quite frequent enough for the
FAQ.)  I can think of two "clean" solutions, both of which require you to
modify your statement a bit.%

[0]  Use the "wrong" type, and cast it.  Here "funcp" can be any function
pointer type (it's going to be used as a generic, and never called); I prefer
to use "void (*)(void)", which in a sense is the simplest.  Adding a second
typedef for the step-type, func_T (*)(void), may make this look nicer.

        typedef void (*func_T)(void);
        func_T (*P)(void);
        func_T f(void) { ... return (func_T)f; }
        for (P=f; P != (func_T (*)(void))0; P = (func_T (*)(void))(*P)());

[1]  Create a recursive type using a one-element struct.  This works because
you *can* define a structure type recursively.  You may want to check whether
your compiler produces efficient code for this.

        typedef struct step_T step_T;
        struct step_T { step_T (*fn)(void); };
        step_T (*P)(void);
        step_T f(void) { step_T st; ... st.fn = f; return st; }
        for (P=f; P != (step_T (*)(void))0; P = (*P)().fn);

Take your pick.


________
% Also, even though "P()" is a legal way to call through a function pointer in
  ANSI C, I'm rewriting it as "(*P)()", partly to make it easier to port to
  pre-ANSI compilers (you'll also have to remove the prototypes, of course),
  and partly because many of us think that maintaining a syntactic distinction
  between functions and function pointers is worth more than the three source
  symbols per call that you save by doing it the other way.



Wed, 12 Oct 1994 04:35:47 GMT  
 Pointer to function returning pointer to function returning...

Quote:
Karl writes:


| % Also, even though "P()" is a legal way to call through a function pointer
|  in ANSI C, I'm rewriting it as "(*P)()", partly to make it easier to port to
|  pre-ANSI compilers (you'll also have to remove the prototypes, of course),
|  and partly because many of us think that maintaining a syntactic distinction
|  between functions and function pointers is worth more than the three source
|  symbols per call that you save by doing it the other way.

 This sentiment I hartily agree with. ANSI C syntax for complicated
function declarations, especially nested or recursive forms, is atrocious.
To attempt to make up for this failing by allowing the omission of three
characters when calling a function via an honest pointer ... golly!




Wed, 12 Oct 1994 07:27:22 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Question about signal()/pointers to functions that return pointers to functions

2. Can function returning pointer return negative integers?

3. Returning Pointer To Pointer From Function

4. Function returning a function-pointer - how to prototype ?

5. Declaring static function returning pointer to extern function

6. About function that returns function pointer

7. Function returning pointers to functions (repost)

8. Function returning pointers to functions

9. How to use (pointer to function), and function and pointer to (pointer to function)

10. (member) function returning pointer to functions like itself?

11. functions returning pointers to functions

12. Function returning a function pointer.

 

 
Powered by phpBB® Forum Software