• Function Pointer (Pointers to Functions)


    helloworld.c


    #include<stdio.h> #include<stdlib.h> void Hello(); void Hello() { printf("Hello World!\n"); } int main() { void (*Hello_ptr)() = Hello; //giving the address of "Hello" funciton to the pointer (*Hello_ptr)(); //ugly way Hello_ptr(); //elegent way to use function pointer }

     Example2.c

    int my_func(int a,int b);
    int your_func(int , int);
    int main()
    {
      int (*funcPtr)(int,int); //declare a function pointer --> not pointing to anything now
      funcPtr = my_func; //initializing function pointer

      //use the function pointer
      int x = (*funcPtr)(5,7);
      //or
      int y = funcPtr(5,7);
    }

    math_pointers.c

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 int sum(int a, int b);
     5 int subtract(int a, int b);
     6 int mul(int a, int b);
     7 int divide(int a, int b);
     8 
     9 int (*p[4]) (int x, int y);
    10 
    11 int main(void)
    12 {
    13   char buf[80];
    14   int result;
    15   int i, j, op;
    16 
    17   p[0] = sum; /* address of sum() */
    18   p[1] = subtract; /* address of subtract() */
    19   p[2] = mul; /* address of mul() */
    20   p[3] = divide; /* address of divide() */
    21 
    22   printf("Enter two numbers: ");
    23   fgets(buf, 80, stdin);
    24   sscanf(buf, "%d %d", &i, &j);
    25   
    26   printf("0: Add, 1: Subtract, 2: Multiply, 3: Div, 4: Quit\n");
    27   printf("Enter number of operation: ");
    28   fgets(buf, 80, stdin);
    29   sscanf(buf, "%d", &op);
    30 
    31   while (op >= 0 && op <= 3)  {
    32     result = (*p[op]) (i, j);
    33     printf("%d\n\n", result);
    34 
    35     printf("0: Add, 1: Subtract, 2: Multiply, 3: Div, 4: Quit\n");
    36     printf("Enter number of operation: ");
    37     fgets(buf, 80, stdin);
    38     scanf(buf, "%d", &op);
    39   }
    40 
    41 
    42   return 0;
    43 }
    44 
    45 int sum(int a, int b)
    46 {
    47   return a + b;
    48 }
    49 
    50 int subtract(int a, int b)
    51 {
    52   return a - b;
    53 }
    54 
    55 int mul(int a, int b)
    56 {
    57   return a * b;
    58 }
    59 
    60 int divide(int a, int b)
    61 {
    62   if (b)
    63   {
    64     return a / b;
    65   }
    66   else
    67   {
    68     return 0;
    69   }
    70 }

     qSort

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 /*
     5 int my_func(void *a, void *b)
     6 {
     7   int retval = 0;
     8 
     9   if (*a > *b)
    10   {
    11     retval = 1;
    12   }
    13   else if (*a < *b)
    14   {
    15     retval = -1;
    16   }
    17   else
    18   {
    19     retval = 0;
    20   }
    21 
    22   return retval;
    23 }
    24 */
    25 
    26 int cmpfunc (const void * a, const void * b)
    27 {
    28    return ( *(int*)a - *(int*)b );
    29 }
    30 
    31 int main()
    32 {
    33   int i;
    34   /* int (*myfuncPtr)(void *,void *);
    35    * myfuncPtr = my_func;
    36    */
    37 
    38   int array[10] = { 34, 33, 54,234,7645, 22,1, 645,345, 237 };
    39 
    40   /* int X = myfuncPtr(37, 24);
    41    * int X = (*myfuncPtr)(37, 24);
    42    */
    43   (*HelloPtr)();
    44 
    45   for (i = 0; i < 10; i++)
    46   {
    47     printf("%d\n", array[i]);
    48   }
    49     
    50   qsort(array, 10, sizeof(int), cmpfunc);
    51 
    52   for (i = 0; i < 10; i++)
    53   {
    54     printf("%d\n", array[i]);
    55   }
    56 }
  • 相关阅读:
    复选框和文字对齐解决方案
    封装getElementsByClassName()
    原生js运动框架
    如何防止鼠标移出移入子元素触发mouseout和mouseover事件
    回调函数
    经验
    sublime text3函数追踪:ctags配置
    笔记
    编程经验NO.1 from月光博客
    概念
  • 原文地址:https://www.cnblogs.com/JasperZhao/p/12812163.html
Copyright © 2020-2023  润新知