functions that return pointer to function

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
 
R

Rod Pemberton

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?

This eliminates your casting. If it doesn't work with your compiler, see
Michael Mair's comments. I commentd out your original code with '#if 0'
'#endif'. Hint: compare the new pf_t with f1.

#include <stdio.h>

typedef void* p_t;
#if 0
typedef void (*pf_t)(void);
typedef pf_t (*ppf_t)(void);
#endif
typedef p_t (*pf_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)
{
#if 0
ppf_t var;
#endif
pf_t var;

#if 0
var = (ppf_t)f1;
var = (ppf_t)var();
var = (ppf_t)var();
var = (ppf_t)var();
#endif
var = f1;
var = var();
var = var();
var = var();

return 0;
}


Rod Pemberton
 
C

Chris Torek

There is none. At least not in portable C.

There is no *direct* type, at least.

[snippage]
Start at f1. f1 returns a pointer to the type of f2.
f2 returns the type of f1, i.e. a pointer to a function
returning the type of f2, ....
The only answer is a generic function pointer type, which
C does not have.

Alternatively, you can wrap these things up inside "struct"s:

#include <stdio.h>

struct func {
struct func (*fp)(void);
};

struct func f1(void), f2(void);

struct func f1(void) {
struct func val;

puts("in f1");
val.fp = f2;
return val;
}

struct func f2(void) {
struct func val;

puts("in f2");
val.fp = f1;
return val;
}

int main(void) {
struct func f = { f1 };
int i;

for (i = 0; i < 5; i++)
f = f.fp();
return 0;
}

(or, do the same as the above but change "struct func" to
"struct func *" in all the obvious places, make f point to one,
and make the "val"s in each function static and return their
addresses; or pass a pointer to the "struct func" object to
each function and have it update f->fp; etc.).

The OP's method (having functions return "pointer to function (args)
returning T" for some valid type T, then using casting to make it all
work out) is OK, if a bit error-prone. In particular, casting tends
to make the compiler shut up even if the cast is wrong, so you have
to very sure that every cast is really correct.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top