
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.