I went to the link http://c-faq.com/decl/recurfuncp.html and tried to understand it.
I have a few questions:
Code:
int (*funcptr)();
states that funcptr is a pointer to a function that takes no arguments and returns an int.
typedef int (*funcptr)();
does this define funcptr as a type? (this is what I think so, but I haven't used typedef like this before )
again
funcptr (*ptrfuncptr)();
states that ptrfuncptr is a pointer to a function that takes no arguments and returns a funcptr.
typedef funcptr (*ptrfuncptr)();
same as before : does this define ptrfuncptr as a type?
I've written the following code, and at least it works :
Code:
#include <stdio.h>
typedef int (*funcptr)(); /* generic function pointer */
typedef funcptr (*ptrfuncptr)(); /* ptr to fcn returning g.f.p. */
int main()
{
funcptr start_function();
ptrfuncptr state = start_function;
while (state != NULL)
state = (ptrfuncptr)(*state)();
return 0;
}
funcptr start_function()
{
static int i=0;
++i;
printf("\nIn function \"start funtion\" for the %dth time!", i);
if(i==5)
return NULL;
else
return start_function;
}
Here the function start-function is being called again and again, (the function itself supplies it's own address) though not recursively, which is obviously not possible as it supplies a pointer to itself in the return statement.
Looking for some comments.
Thanks.
regards,
koodoo.