Function returning a function-pointer - how to prototype ? 
Author Message
 Function returning a function-pointer - how to prototype ?

Hi,

I have a function:

void    *foo(int id, int (*new_fp)(int id, void *param)
{
    int    (*old_fp)(int, void*);

    ...

    return(old_fp);

Quote:
};

My problem is that foo doesn't really return a void*, it returns the
original function-pointer.

So ... how do I write that ?

TIA

--
RH



Tue, 08 Nov 2005 18:53:54 GMT  
 Function returning a function-pointer - how to prototype ?

Quote:

> void    *foo(int id, int (*new_fp)(int id, void *param)
> {
>     int    (*old_fp)(int, void*);

>     ...

>     return(old_fp);
> };

> My problem is that foo doesn't really return a void*, it returns the
> original function-pointer.

> So ... how do I write that ?

int (* <foo(int id, int (*new_fp)(int id, void *param))> )(int, void *)

without the <>'s.  Note how the type matches that of old_fp if you
replace the contents of <> with a variable name.

However, this is pretty unreadable.  The best way is to typedef the
function pointer type to make it more readable:

typedef int (*My_function_t)(int, void*);

My_function_t foo (int id, My_function_t new_fp)
{
    My_function_t old_fp;
    ...
    return(old_fp);

Quote:
};

--
Hallvard


Tue, 08 Nov 2005 19:08:49 GMT  
 Function returning a function-pointer - how to prototype ?

Quote:

> I have a function:

> void    *foo(int id, int (*new_fp)(int id, void *param)
> {
>     int    (*old_fp)(int, void*);

>     ...

>     return(old_fp);
> };

> My problem is that foo doesn't really return a void*, it returns the
> original function-pointer.

> So ... how do I write that ?

Have it return any old function pointer type. All function pointer types
can be safely cast between one another. As long as you cast it back to
the right type before calling it, you can use any function pointer type
for passing a function pointer around.
Note that is is true only for function pointers, not for data pointers,
and certainly not between data and function pointers. You cannot use a
void * (data pointer type) to hold a function pointer.

Richard



Tue, 08 Nov 2005 19:30:06 GMT  
 Function returning a function-pointer - how to prototype ?

Quote:
> Hi,

> I have a function:

> void    *foo(int id, int (*new_fp)(int id, void *param)
> {
>     int    (*old_fp)(int, void*);

>     ...

>     return(old_fp);

Wrong. A (void*) is for objects, not for functions.

Quote:
> };

> My problem is that foo doesn't really return a void*, it returns the
> original function-pointer.

> So ... how do I write that ?

To avoid{*filter*}syntax, use a typedef:

typedef int fun_f(int id, void *param);

fun_f *foo(int id, fun_f *new_fp)
{
   fin_f *old_fp;

   return old_fp;

Quote:
}

I massively use function pointers in my code for output functions. I always
use the typedef. It makes the code readable. It's a pity that the good guys
that have designed the C-language don't use it with 'signal()'.

 void (*signal(int sig, void (*func) (int sig)))(int);

eek!

My proposal:

typedef void signal_f (int sig);

signal_f *signal (int sig, signal_f *func);

Let's breath...

--
-ed- emdel at noos.fr
The C-language FAQ: http://www.*-*-*.com/ ~scs/C-faq/top.html
C-library: http://www.*-*-*.com/
FAQ de f.c.l.c : http://www.*-*-*.com/ ~rumeau/fclc/

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
                          -- Jerry Coffin in a.l.c.c++



Wed, 09 Nov 2005 06:31:00 GMT  
 Function returning a function-pointer - how to prototype ?
On Fri, 23 May 2003 11:53:54 +0100, "Richard Harnden"

Quote:
> Hi,

> I have a function:

> void    *foo(int id, int (*new_fp)(int id, void *param)
> {
>     int    (*old_fp)(int, void*);

>     ...

>     return(old_fp);
> };

> My problem is that foo doesn't really return a void*, it returns the
> original function-pointer.

> So ... how do I write that ?

> TIA

You must have missed this when you read the FAQ for this group, as I
am sure you did before posting here:

http://www.eskimo.com/~scs/C-faq/q1.22.html

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq



Wed, 09 Nov 2005 10:44:40 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Pointer to function returning pointer to function returning...

2. Prototype for function returning funtion pointer

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

4. Function prototypes for pointers to functions.

5. HELP: function prototype with pointer to function as parameter

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

7. Declaring static function returning pointer to extern function

8. About function that returns function pointer

9. Function returning pointers to functions (repost)

10. Function returning pointers to functions

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

12. functions returning pointers to functions

 

 
Powered by phpBB® Forum Software