C typedef is a function prototype with a structure trying to refer to a specific one.

I need to refer to a structure that is not yet defined, because the structure actually encodes a prototype of the typedef'd function.

For example,

typedef int (MyCallbackFunction)(X * x, void * ctx); typedef struct CallbackData { MyCallbackFunction * callback; void * ctx; } CallbackData; typedef struct X { char a; int b; int c; double d; CallbackData e; } X; 

What is a valid way to write this code / header?

+7
source share
3 answers

Just redirect declarations of the corresponding types - and you can make a function pointer in typedef:

 struct X_; typedef int (*MyCallbackFunction)(struct X_ * x, void * ctx); typedef struct CallbackData_ { MyCallbackFunction callback; void * ctx; } CallbackData; typedef struct X_ { char a; int b; int c; double d; CallbackData e; } X; 
+4
source

Just send typedef declaration s

 typedef struct XX; typedef struct CallbackData CallbackData; 

and then declare a struct later.

+5
source

Yes, you can forward the struct declaration and use it in the MyCallbackFunction , where you do not need the full type.

 struct X; typedef int (MyCallbackFunction)(struct X * x, void * ctx); 
0
source

All Articles