devxlogo

Hide Function Pointer Declarations With a typedef

Hide Function Pointer Declarations With a typedef

Can you tell what the following declaration means?

   void (*p[10]) (void (*)() );

Only few programmers can tell that p is an “array of 10 pointers to a function returning void and taking a pointer to another function that returns void and takes no arguments.” The cumbersome syntax is nearly indecipherable. However, you can simplify it considerably by using typedef declarations. First, declare a typedef for “pointer to a function returning void and taking no arguments” as follows:

   typedef void (*pfv)();

Next, declare another typedef for “pointer to a function returning void and taking a pfv” based on the typedef we previously declared:

   typedef void (*pf_taking_pfv) (pfv);

Now that we have created the pf_taking_pfv typedef as a synonym for the unwieldy “pointer to a function returning void and taking a pfv”, declaring an array of 10 such pointers is a breeze:

   pf_taking_pfv p[10];

devx-admin

Share the Post: