我建议使用以下方法:
typedef struct xyz // xyz is a struct tag, not a type
{
int a;
int b;
void (*callback) (struct xyz* p); // this definition is local, "temporary" until the function pointer typedef below
} xyz_t; // xyz_t is a type
typedef void (*callback_t) (xyz_t* p);
现在,就调用者而言,您的结构等同于:
// This is pseudo-code used to illustrate, not real C code
typedef struct
{
int x;
int y;
callback_t callback;
} xyz_t;
使用示例:
void func (xyz_t* ptr)
{ ... }
int main()
{
xyz_t some_struct =
{
1, // a
2, // b
&func // callback
};
xyz_t another_struct =
{ ... };
some_struct.callback (&another_struct); // calls func
}