指向函数的指针的理解问题

指向函数的指针的理解问题

原题如下:

calculate形参中函数指针部分:应该理解为第三个参数为一个指向a function which return double and need one double 该指针叫hanshu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>
double for_y1(double x)
{
return 1+x*x;
}
double for_y2(double x)
{
return x/(1+x*x);
}
double calculate(double a,double b,double (*hanshu)(double))
{
double n=10000;//n为等分数
double h=(b-a)/n;
double sum=((*hanshu)(a)+(*hanshu)(b))/2;
for(int i=1;i<n;i++)
{
sum+=(*hanshu)(a+i*h);//(*hanshu)表示取hanshu指针指向的函数
}
return sum*h;
}
int main(void)
{
double a,b;
scanf("%lf %lf",&a,&b);
printf("%lf %lf", calculate(a, b, for_y1), calculate(a, b, for_y2));
return 0;
}