It is currently Tue, 07 Dec 2021 03:00:51 GMT



 
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;

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;

Or

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

Or

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

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.


 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
c...@crash.cts.com



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

Similar Threads

1. Function pointer to member functions

2. function calls versus function pointers ?

3. function, function...who's got the function?

4. Array of function pointers

5. C++ compiler 5.3 can't compile RW vector of member function pointers

6. Function Pointer cast problem

7. Function pointer in Solaris kernel programming.

8. Pointer to Member functions (C++ problem)

9. gcc, ld, function pointers, and shared libraries


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software