Page 186 - C-Language
P. 186

}

         int Multiply(int i, int j){
             return i*j;
         }

         int main()
         {
             ptrInt ptr1 = Add;
             ptrInt ptr2 = Multiply;

             printf("%d\n", (*ptr1)(2,3)); //will print 5
             printf("%d\n", (*ptr2)(2,3)); //will print 6
             return 0;
         }


        You can also create an Array of function-pointers. If all the pointers are of the same "structure":


         int (*array[2]) (int x, int y); // can hold 2 function pointers
         array[0] = Add;
         array[1] = Multiply;


        You can learn more here and here.


        It is also possible to define an array of function-pointers of different types, though that would
        require casting when-ever you want to access the specific function. You can learn more here.


        Read Function Pointers online: https://riptutorial.com/c/topic/250/function-pointers
















































        https://riptutorial.com/                                                                             162
   181   182   183   184   185   186   187   188   189   190   191