
Function returning pointers to functions (repost)
Hi,
Sorry guys for my latest code about functions returning pointers to
functions full with syntax errors.
I have repaired those syntax errors, and after some changes my code now
looks like:
<----------------- this is line #1
static void f1()
{
printf(" this is function f1 \n");
Quote:
}
static void f2()
{
printf(" this is function f2 \n");
Quote:
}
static void (*getFunc(int i))
{
switch (i)
{
case 1:
return &f1; <----------------- this is line #17
break;
case 2:
return &f2; <----------------- this is line #20
break;
}
Quote:
}
int main(void)
{
void (*fP)();
fP = getFunc(2); <----------------- this is line #28
fP();
Quote:
}
and it does compile, but anyone who can explain what those warnings mean ?
"test.c", line 17: warning: return value type mismatch
"test.c", line 20: warning: return value type mismatch
"test.c", line 28: warning: assignment type mismatch:
pointer to function() returning void "=" pointer to void
It even runs as expected,
[~/tst] sunray : cc test.c
[~/tst] sunray : a.out
this is function f2
It would be nice to understand what those warnings refer to in order to
implement a much more complicated function of that type.
BTW, what does the line of code :
static void (*getFunc(int i))
mean ?
Is that a function returning a pointer to a void and expecting an integer as
argument, or a pointer to a function expecting an integer as argument and
returning a normal void (no address).
And is that the same as :
static (void *) getFunc(int i)
and if not: what does this declaration mean ?
Thanks a lot !
Anthony