转移表

#include <stdio.h>int Add(int x, int y){    return x + y;}int Sub(int x, int y){    return x - y;}int Mul(int x, int y){    return x * y;}int Div(int x, int y){    return x / y;}void menu(){    printf("************************************\n");    printf("****  1.ADD              2.SUB  ****\n");    printf("****  3.MUL              4.DIV  ****\n");    printf("**************0.EXIT****************\n");}int main(){    int input = 0;    int x = 0;    int y = 0;    int(*pfArr[5])(int, int) = { 0,Add,Sub,Mul,Div };    do    {        menu();        printf("请选择:>");        scanf("%d", &input);        if (input > 0 && input < 5)        {            printf("请输入一组数字:> ");            scanf("%d%d", &x, &y);            printf("%d\n", (*pfArr[input])(x, y));        }        else if (input == 0)        {            printf("计算器退出\n");        }        else        {            printf("非法输入,请重新输入\n");        }    } while (input);    return 0;}

这是函数指针的一个应用,确实是可以在某些情况下方便使用




指向函数指针数组的指针

#include <stdio.h>int main(){    int arr[10] = { 0 };    int(*p)[10]=&arr;//取出了数组的地址    int (*pfArr)(int, int);//函数指针    int (*pfArr[4])(int, int);//定义一个数组 - 函数指针数组    int (*(*ppfArr)[4])(int, int) = &pfArr;    //ppfArr是一个指向[函数指针数组]的指针    //ppfArr是一个指针,指向一个数组,数组里有四个元素    //指向的数组里每一个元素的类型都是一个函数指针,int(*)(int,int)    return 0;}

函数有对应的地址,多个函数就能对应多个函数指针,同种类型的元素集合在一起就成了数组,于是又有了指向函数指针数组的指针,然后多个指向函数数组的指针在一起就能有指向函数指针数组的指针的数组,然后就又能有指向函数指针数组的指针的数组的指针...




回调函数 - qsort的使用

//通过一个函数指针调用的函数//void* 可以存储任意类型的指针//void*不能进行解引用操作//void*不能进行加减整数的操作#include <stdio.h>#include <stdlib.h>#include <string.h>int cmp_int(const void* e1, const void* e2){    return *(int*)e1 - *(int*)e2;}void test1(){    int arr[] = { 9,8,7,6,5,4,3,2,1,0 };    int sz = sizeof(arr) / sizeof(arr[0]);    qsort(arr, sz, sizeof(arr[0]), cmp_int);    int i = 0;    for (i = 0; i < sz; i++)    {        printf("%d ", arr[i]);    }}int com_f(const void* e1, const void* e2){    return *(float*)e1 - *(float*)e2;}void test2(){    float arr[] = { 1.2,2.1,3.5,3.5,4.8,8.9,7.8,5.6,6.5 };    int sz = sizeof(arr) / sizeof(arr[0]);    qsort(arr, sz, sizeof(arr[0]), com_f);    int i = 0;    for (i = 0; i < sz; i++)    {        printf("%f ", arr[i]);    }}struct Stu{    char name[20];    int age;};int com_stu_by_age(const void* e1, const void* e2){    return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;}int com_stu_by_name(const void* e1, const void* e2){    return strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name);}void test3(){    struct Stu s[3] = { {"zhangsan",60},{"lisi",40},{"wangwu",50} };    int sz = sizeof(s) / sizeof s[0];    //qsort(s, sz, sizeof(s[0]), com_stu_by_age);    //int i = 0;    //for (i = 0; i < sz; i++)    //{    //  printf("%d ", *(&s[i].age));    //}    printf("\n");    qsort(s, sz, sizeof(s[0]), com_stu_by_name);    int j = 0;    for (j = 0; j < sz; j++)    {        printf("%s ", &s[j].name);    }}int main(){    //test1();    //printf("\n");    //test2();    //printf("\n");    test3();    return 0;}



qsort的演示程序

#include <stdio.h>#include <stdlib.h>#include <limits.h>int compare_ints(const void* a, const void* b){    int arg1 = *(const int*)a;    int arg2 = *(const int*)b;    if (arg1 < arg2) return -1;    if (arg1 > arg2) return 1;    return 0;    // return (arg1 > arg2) - (arg1 < arg2); // 可行的简写    // return arg1 - arg2; // 错误的简写(若给出 INT_MIN 则会失败)}int main(void){    int ints[] = { -2, 99, 0, -743, 2, INT_MIN, 4 };    int size = sizeof ints / sizeof * ints;    qsort(ints, size, sizeof(int), compare_ints);    for (int i = 0; i < size; i++)     {        printf("%d ", ints[i]);    }    printf("\n");}
©著作权归作者所有:来自51CTO博客作者赤晴的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. 数组排序-合并-成员统计-交差并补
  2. PHP常用的数组函数
  3. 数组函数练习
  4. php数组的排序|数组的合并
  5. php学习笔记(数组函数_差集和交集的运算汇总)
  6. 0126-数据类型的转换与检测技术,变量与常用声明及使用
  7. 数据类型的转换、变量与常量声明和使用
  8. php常用数组函数以及数组的交集,差集,并集
  9. 数组的排序, 数组的合并, 数组成员的统计

随机推荐

  1. ImportNew 一周资讯 : 2018 值得关注的 9
  2. pmp考试总结
  3. 铁定不纯的IO_Haskell笔记5
  4. Android(安卓)JNI开发入门之二
  5. HTTP2和HTTPS来不来了解一下?
  6. Java.nio VS Java.io
  7. 使用 Thread Pool 不当引发的死锁
  8. Bash On Ubuntu On Windows折腾记
  9. Java 中的构造函数引用和方法引用
  10. 深入typeclass_Haskell笔记4