Page 182 - C-Language
P. 182

It might be handy to use a typedef instead of declaring the function pointer each time by hand.


        The syntax for declaring a typedef for a function pointer is:


         typedef returnType (*name)(parameters);


        Example:



        Posit that we have a function, sort, that expects a function pointer to a function compare such that:


              compare - A compare function for two elements which is to be supplied to a sort
              function.

              "compare" is expected to return 0 if the two elements are deemed equal, a positive
              value if the first element passed is "larger" in some sense than the latter element and
              otherwise the function returns a negative value (meaning that the first element is
              "lesser" than the latter).


        Without a typedef we would pass a function pointer as an argument to a function in the following
        manner:


         void sort(int (*compare)(const void *elem1, const void *elem2)) {
             /* inside of this block, the function is named "compare" */
         }


        With a typedef, we'd write:


         typedef int (*compare_func)(const void *, const void *);


        and then we could change the function signature of sort to:


         void sort(compare_func func) {
             /* In this block the function is named "func" */
         }


        both definitions of sort would accept any function of the form


         int compare(const void *arg1, const void *arg2) {
             /* Note that the variable names do not have to be "elem1" and "elem2" */
         }



        Function pointers are the only place where you should include the pointer property of the type, e.g.
        do not try to define types like typedef struct something_struct *something_type. This applies even
        for a structure with members which are not supposed to accessed directly by API callers, for
        example the stdio.h FILE type (which as you now will notice is not a pointer).








        https://riptutorial.com/                                                                             158
   177   178   179   180   181   182   183   184   185   186   187