Pedro Perez
Pedro Perez

Reputation: 330

Strange typedef to function pointer

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.

Upvotes: 4

Views: 793

Answers (1)

templatetypedef
templatetypedef

Reputation: 372784

This first typedef

typedef void (myType)(void);

provides myType as a synonym for the type void (void), the type of a function that takes no arguments and returns void. The parentheses around myType aren't actually necessary here; you could also write

typedef void myType(void);

to make it clearer that it's the type of a function that takes void and returns void. 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 typedef

typedef myType *myTypePtr;

then says that myTypePtr has a type that's equal to a pointer to a myType, which means that it's a pointer to a function that takes no arguments and returns void. This new type is equivalent to the type void (*)(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!

Upvotes: 4

Related Questions