Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
690 views
in Technique[技术] by (71.8m points)

c - How can I typedef a function pointer that takes a function of its own type as an argument?

Example: A function that takes a function (that takes a function (that ...) and an int) and an int.

typedef void(*Func)(void (*)(void (*)(...), int), int);

It explodes recursively where (...). Is there a fundamental reason this can't be done or is there another syntax? It seems to me it should be possible without a cast. I'm really trying to pass a dispatch-table but I could figure that out if I could just pass this one type.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can wrap the function pointer in a struct:

struct fnptr_struct;
typedef void (*fnptr)(struct fnptr_struct *);
struct fnptr_struct {
  fnptr fp;
};

I'm not sure if this is an improvement on casting. I suspect that it's impossible without the struct because C requires types to be defined before they are used and there's no opaque syntax for typedef.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...