C Tutorial/Function/Function Pointer

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Arrays of Pointers to functions

#include <stdio.h>
int sum(int x, int y){
  return x + y;
}
int product(int x, int y){
  return x * y;
}
int difference(int x, int y){
  return x - y;
}
int main(void){
  int a = 10;
  int b = 5;
  int result = 0;
  int (*pfun[3])(int, int);           /* Function pointer array declaration */
  /* Initialize pointers */
  pfun[0] = sum;
  pfun[1] = product;
  pfun[2] = difference;
  int i;
  for(i = 0 ; i < 3 ; i++)
  {
    result = pfun[i](a, b);           /* Call the function through a pointer */
    printf("\nresult = %d", result);  /* Display the result                  */
  }
  return 0;
}
result = 15
     result = 50
     result = 5

Passing a pointer of a function to another function

/*
Beginning C: From Novice to Professional, Fourth Edition
By Ivor Horton
ISBN: 1-59059-735-4
640 pp.
Published: Oct 2006
*/

#include <stdio.h>
int any_function(int(*pfun)(int, int), int x, int y){
  return pfun(x, y);
}
int sum(int x, int y){
  return x + y;
}
int product(int x, int y){
  return x * y;
}
int difference(int x, int y){
  return x - y;
}
int main(void){
  int a = 10; 
  int b = 5;  
  int result = 0; 
  int (*pf)(int, int) = sum;          /* Pointer to sum function */
  result = any_function(pf, a, b);
  printf("\nresult = %d", result );
  result = any_function(product,a, b);
  printf("\nresult = %d", result );
  printf("\nresult = %d\n", any_function(difference, a, b));
  return 0;
}
result = 15
     result = 50
     result = 5

Pointing to functions

#include <stdio.h>
int sum(int x, int y)
{
  return x + y;
}

int product(int x, int y)
{
  return x * y;
}

int difference(int x, int y)
{
  return x - y;
}

int main(void)
{
  int a = 10;
  int b = 5; 
  int result = 0;
  int (*pfun)(int, int);              /* Function pointer declaration      */
  pfun = sum;             
  result = pfun(a, b);                /* Call sum() through pointer        */
  printf("\npfun = sum             result = %d", result);
  pfun = product;         
  result = pfun(a, b);                /* Call product() through pointer    */
  printf("\npfun = product         result = %d", result);
  pfun = difference;      
  result = pfun(a, b);                /* Call difference() through pointer */
  printf("\npfun = difference      result = %d\n", result);
  return 0;
}
pfun = sum             result = 15
     pfun = product         result = 50
     pfun = difference      result = 5

Using array of pointers to functions in one statement

#include <stdio.h>
int sum(int x, int y){
  return x + y;
}
int product(int x, int y){
  return x * y;
}
int difference(int x, int y){
  return x - y;
}
int main(void){
  int a = 10;               
  int b = 5;                
  int result = 0;           
  int (*pfun[3])(int, int);           /* Function pointer array declaration */
  /* Initialize pointers */
  pfun[0] = sum;
  pfun[1] = product;
  pfun[2] = difference;
  /* Call all three functions through pointers in an expression */
  result = pfun[1](pfun[0](a, b), pfun[2](a, b));
  printf("\n\nThe product of the sum and the difference = %d\n",
                                                           result);
  return 0;
}
The product of the sum and the difference = 75