Page 181 - C-Language
P. 181

return 0;
         }


        Returning Function Pointers from a Function



         #include <stdio.h>

         enum Op
         {
           ADD = '+',
           SUB = '-',
         };


         /* add: add a and b, return result */
         int add(int a, int b)
         {
             return a + b;
         }

         /* sub: subtract b from a, return result */
         int sub(int a, int b)
         {
             return a - b;
         }

         /* getmath: return the appropriate math function */
         int (*getmath(enum Op op))(int,int)
         {
             switch (op)
             {
                 case ADD:
                     return &add;
                 case SUB:
                     return ⊂
                 default:
                     return NULL;
             }
         }

         int main(void)
         {
             int a, b, c;
             int (*fp)(int,int);

             fp = getmath(ADD);

             a = 1, b = 2;
             c = (*fp)(a, b);
             printf("%d + %d = %d\n", a, b, c);
             return 0;
         }


        Best Practices



        Using typedef





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