M
msolem
I have some code where there are a set of functions that return
pointers to each other. I'm having a bit of a hard time figuring out
the correct type to use to do that.
The code below works but I'm defining the functions as void*, and then
casting when I use them. This code is going into a general purpose
framework, and it would be much nicer if the user didn't need to do
any casting.
Can someone tell me how to set up those typedefs so that the casting
done in main is not required?
Thanks,
Mike
=================================
#include <stdio.h>
typedef void* p_t;
typedef void (*pf_t)(void);
typedef pf_t (*ppf_t)(void);
p_t f1(void);
p_t f2(void);
p_t f1(void)
{
printf("func1\n");
return f2;
}
p_t f2(void)
{
printf("func2\n");
return f1;
}
int main (void)
{
ppf_t var;
var = (ppf_t)f1;
var = (ppf_t)var();
var = (ppf_t)var();
var = (ppf_t)var();
return 0;
}
=================================
The output is:
func1
func2
func1
pointers to each other. I'm having a bit of a hard time figuring out
the correct type to use to do that.
The code below works but I'm defining the functions as void*, and then
casting when I use them. This code is going into a general purpose
framework, and it would be much nicer if the user didn't need to do
any casting.
Can someone tell me how to set up those typedefs so that the casting
done in main is not required?
Thanks,
Mike
=================================
#include <stdio.h>
typedef void* p_t;
typedef void (*pf_t)(void);
typedef pf_t (*ppf_t)(void);
p_t f1(void);
p_t f2(void);
p_t f1(void)
{
printf("func1\n");
return f2;
}
p_t f2(void)
{
printf("func2\n");
return f1;
}
int main (void)
{
ppf_t var;
var = (ppf_t)f1;
var = (ppf_t)var();
var = (ppf_t)var();
var = (ppf_t)var();
return 0;
}
=================================
The output is:
func1
func2
func1