zoukankan      html  css  js  c++  java
  • 函数指针小结

    1. 函数指针指指向函数的指针,强调指针,函数名是内存中的起始地址。

      格式为:int (*fun)(int,int)。

    2. 指针函数强调函数,指返回值是指针类型的函数。指针函数常见,不再具体说明。

      格式为:int *fun(int,int);

      #include <stdio.h>
               
      #define N 10
               
      /*函数指针指指向函数的指针,强调指针,函数名是内存中的起始地址。
      格式为:int (*fun)(int,int)。
      而指针函数强调函数,指返回值是指针类型的函数。
      格式为:int *fun(int,int);*/
      void swap(int *x,int *y)//交换
      {
          int t = *x;
          *x = *y;
          *y = t;
      }
               
      int asc(int a,int b)//递增
      {
          return a > b ? 1 : 0;
      }
               
      int des(int a,int b)//递减
      {
          return a < b ? 1 : 0;
      }
               
      void bubblesort(int a[],int n,int (*compare) (int,int))//函数指针
      {
          int i,j;
          for(i = 0; i< n;i++)
          {
              for(j = 0; j < n-i-1;j++)
              {
                  if((*compare)(a[j],a[j+1]))
                      swap(&a[j],&a[j+1]);
              }
          }
      }
               
      void display(int a[],int n)
      {
          int i;
          for(i = 0; i< n;i++)
              printf("%-4d",a[i]);
          printf("
      ");
      }
               
      int main()
      {
          int a[N] = {2,1,5,3,4,7,8,6,10,9};
          bubblesort(a,N,asc);
          display(a,N);
          bubblesort(a,N,des);
          display(a,N);
          return 0;
      }
  • 相关阅读:
    【BZOJ1135】[POI2009]Lyz
    【BZOJ5110】[CodePlus2017]Yazid 的新生舞会
    「BZOJ2882」工艺
    【BZOJ3626】[LNOI2014]LCA
    [Wc]Dface双面棋盘()
    【BZOJ3307】雨天的尾巴
    [NOI2012]骑行川藏
    【BZOJ4919】[Lydsy六月月赛]大根堆
    bzoj4184
    3237: [Ahoi2013]连通图 线段树分治
  • 原文地址:https://www.cnblogs.com/zxy1992/p/3452400.html
Copyright © 2011-2022 走看看