Understanding typedefs for function pointers in C

后端 未结 7 1699
别跟我提以往
别跟我提以往 2020-11-22 11:16

I have always been a bit stumped when I read other peoples\' code which had typedefs for pointers to functions with arguments. I recall that it took me a while to get around

7条回答
  •  暖寄归人
    2020-11-22 11:31

    This is the simplest example of function pointers and function pointer arrays that I wrote as an exercise.

        typedef double (*pf)(double x);  /*this defines a type pf */
    
        double f1(double x) { return(x+x);}
        double f2(double x) { return(x*x);}
    
        pf pa[] = {f1, f2};
    
    
        main()
        {
            pf p;
    
            p = pa[0];
            printf("%f\n", p(3.0));
            p = pa[1];
            printf("%f\n", p(3.0));
        }
    

提交回复
热议问题