C/Function/Function Pointer

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

Array of function pointer

#include <stdio.h>
int sum(int a, int b);
int subtract(int a, int b);
int mul(int a, int b);
int div(int a, int b);
int (*p[4]) (int x, int y);
int main(void)
{
  int result;
  int i, j, op;
  p[0] = sum; /* address of sum() */
  p[1] = subtract; /* address of subtract() */
  p[2] = mul; /* address of mul() */
  p[3] = div; /* address of div() */
  printf("Enter two numbers: ");
  scanf("%d %d", &i, &j);
  
  printf("0: Add, 1: Subtract, 2: Multiply, 3: Divide\n");
  do {
    printf("Enter number of operation: ");
    scanf("%d", &op);
  } while(op<0 || op>3);
  result = (*p[op]) (i, j);
  printf("%d", result);
  return 0;
}
int sum(int a, int b)
{
  return a + b;
}
int subtract(int a, int b)
{
  return a - b;
}
int mul(int a, int b)
{
  return a * b;
}
int div(int a, int b)
{
  if(b) 
      return a / b;
  else 
      return 0;
}


Arrays of Pointers to functions

#include <stdio.h>
int sum(int, int);
int product(int, int);
int subtract(int, int);
int main()
{
   int i = 0;
   int a = 10;
   int b = 5;
   int result = 0;
   int (*pfun[3])(int, int);
   pfun[0] = sum;
   pfun[1] = product;
   pfun[2] = subtract;
   for( i = 0 ; i < 3 ; i++)
   {
     result = pfun[i](a, b);
     printf("\nresult = %d", result);
   }
   result = pfun[1](pfun[0](a, b), pfun[2](a, b));
   printf("\n\nThe product of the sum and the subtract = %d\n",result);
}
int sum(int x, int y)
{
   return x + y;
}
int product(int x, int y)
{
   return x * y;
}
int subtract(int x, int y)
{
   return x - y;
}


Function pointer

#include <stdio.h>
#include <string.h>
void check(char *a, char *b,int (*cmp)(const char *, const char *));
int main(void)
{
  char s1[80]= "asdf", s2[80]="asdf";
  int (*p)(const char *, const char *);
  p = strcmp;

  check(s1, s2, p);
  return 0;
}
void check(char *a, char *b,int (*cmp)(const char *, const char *))
{
  printf("Testing for equality.\n");
  if(!(*cmp)(a, b)) {
     printf("Equal");
  }else {
     printf("Not Equal");
  }
}


Function pointer 2

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
int compvalues(const char *a, const char *b);
void check(char *a, char *b,int (*cmp)(const char *, const char *));
int main(void)
{
  char s1[80]="123", s2[80]="123";
  printf("Testing values for equality.\n");
  check(s1, s2, compvalues);

  return 0;
}
void check(char *a, char *b,int (*cmp)(const char *, const char *))
{
  if(!(*cmp)(a, b))
     printf("Equal");
  else
     printf("Not Equal");
}
int compvalues(const char *a, const char *b)
{
  if(atoi(a)==atoi(b))
     return 0;
  else
     return 1;
}


Function pointer and use it call a function

#include <stdio.h>
int sum(int a, int b);
int main(void)
{
  int (*p) (int x, int y);
  int result;
  p = sum; /* get address of sum() */
  result = (*p) (10, 20);
  printf("%d", result);
  return 0;
}
int sum(int a, int b)
{
  return a+b;
}


Function pointer: function call

  
#include <stdio.h>
int *f(int x);
int count;
int main(void)
{
  int *p;
  p = f(110); /* return pointer */
  printf("count (through p) is %d", *p);
  return 0;
}
int *f(int x)
{
  count = x;
  return &count; /* return a pointer */
}


Implementing arithmetic and array functions

#include <stdio.h>

double add(double a, double b)
{
  return a+b;
}
double subtract(double a, double b)
{
  return a-b;
}
double multiply(double a, double b)
{
  return a*b;
}
double array_op(double array[], int size, double (*pfun)(double,double))
{
  double result = array[size-1];
  int i = 0;
  for(i = size-1 ; i>0 ; i--)
    result = pfun(array[i-1], result);
  return result;
}

int main()
{
  double array[] = {11.0, 10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0};
  int i = 0;
  int length = sizeof array/sizeof(double);
  printf("The value of:\n");
  for(i = 0 ; i< length ; i++)
  {
    printf("%.2f%s\n", array[i]);
  }
  printf(" is %.2lf\n", array_op(array,length,add));
  printf("\nThe value of:\n");
  for(i = 0 ; i< length ; i++)
  {
    printf("%.2lf%s\n", array[i]);
  }
  printf(" is %.2lf\n", array_op(array, length, subtract));
  printf("\nThe value of:\n");
  for(i = 0 ; i< length ; i++)
  {
    printf("%.2lf%s\n", array[i]);
  }
  printf(" is %.2lf\n", array_op(array, length, multiply));
}


Initialize the function pointer array

#include <stdio.h>
int sum(int a, int b);
int subtract(int a, int b);
int mul(int a, int b);
int div(int a, int b);
/* initialize the pointer array */
int (*p[4]) (int x, int y) = {
  sum, subtract, mul, div
} ;
int main(void)
{
  int result;
  int i = 2, j = 2, op;
  
  printf("0: Add, 1: Subtract, 2: Multiply, 3: Divide\n");
  do {
    printf("Enter number of operation: ");
    scanf("%d", &op);
  } while(op<0 || op>3);
  result = (*p[op]) (i, j);
  printf("%d", result);
  return 0;
}
int sum(int a, int b)
{
  return a + b;
}
int subtract(int a, int b)
{
  return a - b;
}
int mul(int a, int b)
{
  return a * b;
}
int div(int a, int b)
{
  if(b) 
      return a / b;
  else 
      return 0;
}


Passing a Pointer to a function

#include <stdio.h>
int sum(int,int);
int product(int,int);
int difference(int,int);
int any_function(int(*pfun)(int, int), int x, int y);
int main()
{
   int a = 13;
   int b = 51;
   int result = 0;
   int (*pf)(int, int) = sum;
   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));
}
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;
}


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 main()
{
   int a = 13;
   int b = 5;
   int result = 0;
   int (*pfun)(int, int);
   pfun = sum;
   result = pfun(a, b);
   printf("\npfun = sum   result = %d", result);
   pfun = product;
   result = pfun(a, b);
   printf("\npfun = product         result = %d", result);
}