Invoke a Function Using a Pointer-to-Function - C Pointer

C examples for Pointer:Function Pointer

Description

Invoke a Function Using a Pointer-to-Function

Demo Code

#include <stdio.h>

int sum(int n1, int n2);
int add(int m1, int m2);

int main()//from w w  w  .j  ava  2  s  .c om
{
   int r;
   int(*ptrFunc)( int, int );

   ptrFunc = sum;
   r = (*ptrFunc)(2, 4);
   printf("(int)(2 + 4) = %d\n", r);

   ptrFunc = add;
   r = (*ptrFunc)(10, 15);
   printf("10 + 15 = %d\n", r);

   return(0);
}

int sum(int j1, int j2)
{
   int result;
   result = (int)(j1 + j2);
   return(result);
}

int add(int k1, int k2)
{
   return(k1 + k2);
}

Result


Related Tutorials