Strange typedef for pointer function

I am using code written by someone else where they intend to use a function pointer. They make a very strange typdef, which I cannot understand. Below code

typedef void (myType)(void); typedef myType *myTypePtr; 

I 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 it? Function type? Me.

In addition, a prototype of this function later exists.

 int createData(int id,int *initInfo, myTypePtr startAddress) 

However, I get a compilation error "expected declaration qualifiers or" ... "before" myTypePtr ", any idea why this is happening? Thanks a lot.

+4
source share
1 answer

This first typedef

 typedef void (myType)(void); 

provides myType as a synonym for type void (void) , a type of function that takes no arguments and returns void . The brackets around myType not really needed; you can also write

 typedef void myType(void); 

to clarify that this is the type of function that takes void and returns void . Note: you cannot actually declare any type variables of a function; the only way to get an object of type function in C is to define the actual function.

Second typedef

 typedef myType *myTypePtr; 

then says that myTypePtr is of type equal to a pointer to myType , which means that it is a pointer to a function that takes no arguments and returns void . This new type is equivalent to the void (*)(void) , but is done a little indirectly.

As for your second mistake, I can’t say for sure that without too much context. Please post a minimal test case so that we can see what causes the error.

Hope this helps!

+4
source

All Articles