About function that returns function pointer 
Author Message
 About function that returns function pointer

nclude <stdio.h>

typedef int     (*func_t)(int);

extern int      square(int);
extern func_t get_func_pointer(void);

int
main(void)
{
        func_t  fp;     /* Can be changed to 'int (*fp)(int);' */

        fp = get_func_pointer();
        printf("The square of %d is %d\n", 10, fp(10));
        return 0;

Quote:
}

int
square(int a)
{
        return a * a;

Quote:
}

func_t                          /* XXX */
get_func_pointer(void)
{
        return square;

Quote:
}

=========
I wrote above test program in order to test a function, that returns
the function pointer of another function, like get_func_pointer(void).

I was tempted to change the /* XXX */ line to its equivalent line,
I mean, I'd tried to change the get_func_pointer(void) into following
lines.

(int (*)(int)) /* WRONG !!! */
get_func_pointer(void)
{
        return square;

Quote:
}

Or

int (*)(int) /* WRONG !!! */
get_func_pointer(void)
{
        return square;

Quote:
}

Or

int *(int) /* WRONG !!! */
get_func_pointer(void)
{
        return square;

Quote:
}

All the above three get_func_pointer() gave me 'syntax error's.
Is there anybody who can give me the 'equilvant line' of the above 'XXX'
line?  I think you surely understand what I mean the 'equivalent line'.

=========
please dont send me by my email address, I email system has some problems.



Tue, 18 Sep 2001 03:00:00 GMT  
 About function that returns function pointer
I think you'll find it's
int (*get_func_pointer(void ))(int )
Seeing as you've also posted to unix groups, here's something you
might find useful.
There's a utility called cdecl which can convert from english
declarations to C and vice versa.
(I can never remember myself how to declare functions returning
function pointers - I always run to this program.)
What I typed was
'declare get_func_pointer as function (void) returning pointer to
 function (int) returning int'
and it gave the above C declaration.
manuel.
I should point out that I don't know how 'standard' this utility is -
It's part of the default Linux C development distribution, and thats
good enough for me.
Quote:

> =========
> I wrote above test program in order to test a function, that returns
> the function pointer of another function, like get_func_pointer(void).

> I was tempted to change the /* XXX */ line to its equivalent line,
> I mean, I'd tried to change the get_func_pointer(void) into following
> lines.

> (int (*)(int)) /* WRONG !!! */
> get_func_pointer(void)
> {
>         return square;
> }

> Or



Tue, 18 Sep 2001 03:00:00 GMT  
 About function that returns function pointer
[|>func_t                          /* XXX */

[|>I was tempted to change the /* XXX */ line to its equivalent line,
[|>I mean, I'd tried to change the get_func_pointer(void) into following

"Doctor! Doctor! It hurts when I do this!"
"Then don't do that."

The typedef lets you use a nice, pausibly meaningful, symbolic name.
Abandonning it will do nothing but burn out a few neurons.

--
Fe mhats enearha esma; iufue dolha soentrides odoem esri.
Fe bhuearai osraha esma; iufue auaen bhuearai shahem essa.
CACS: http://homestead.dejanews.com/user.smjames/index.html
text: http://www.geocities.com/SoHo/Studios/5079/index.html
Megagilby's inflatable fantasy joy-toy and pinata.



Tue, 18 Sep 2001 03:00:00 GMT  
 About function that returns function pointer
: nclude <stdio.h>

: typedef int     (*func_t)(int);

: extern int      square(int);
: extern func_t get_func_pointer(void);

: int
: main(void)
: {
:         func_t  fp;     /* Can be changed to 'int (*fp)(int);' */

:         fp = get_func_pointer();
:         printf("The square of %d is %d\n", 10, fp(10));
:         return 0;
: }

: int
: square(int a)
: {
:         return a * a;
: }

: func_t                          /* XXX */
: get_func_pointer(void)
: {
:         return square;
: }

: =========
: I wrote above test program in order to test a function, that returns
: the function pointer of another function, like get_func_pointer(void).

: I was tempted to change the /* XXX */ line to its equivalent line,
: I mean, I'd tried to change the get_func_pointer(void) into following
: lines.

: (int (*)(int)) /* WRONG !!! */
: get_func_pointer(void)
: {
:         return square;
: }

int (*get_func_pointer(void))(int) { return square; }

It reads from inside out, which can be tricky.

Will



Tue, 18 Sep 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

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

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

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

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

5. Declaring static function returning pointer to extern function

6. Function returning pointers to functions (repost)

7. Function returning pointers to functions

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

9. functions returning pointers to functions

10. Function returning a function pointer.

11. Function returning pointer to a function

12. Function pointer as a return value of a function

 

 
Powered by phpBB® Forum Software