I am using a code written by somebody else, where they intend to use a function pointer. They do a very strange typdef that I can not understand. Below the code
typedef void (myType)(void);
typedef myType *myTypePtr;
I can understand that the main idea with myTypePtr is to create a “pointer to a function that receives void and returns void. But what about the original myType? What is that? a function type? Is not clear to me.
Furthermore, later there is this function prototype
int createData(int id,int *initInfo, myTypePtr startAddress)
However I get the compile error “expected declaration specifiers or ‘…’ before ‘myTypePtr’ any idea why this is happening?. Thank you very much.
This first
typedefprovides
myTypeas a synonym for the typevoid (void), the type of a function that takes no arguments and returnsvoid. The parentheses aroundmyTypearen’t actually necessary here; you could also writeto make it clearer that it’s the type of a function that takes
voidand returnsvoid. Note that you can’t actually declare any variables of function type; the only way to get an object of function type in C is to define an actual function.The second
typedefthen says that
myTypePtrhas a type that’s equal to a pointer to amyType, which means that it’s a pointer to a function that takes no arguments and returnsvoid. This new type is equivalent to the typevoid (*)(void), but is done a bit indirectly.As for your second error, I can’t say for certain what’s up without more context. Please post a minimal test case so that we can see what’s causing the error.
Hope this helps!