预期'double(*)(double)'但参数在C

时间:2017-03-24 19:55:45

标签: c

我需要帮助,我试图用C中的梯形规则计算函数的积分。

我试图在另一个函数中使用函数作为变量但是我遇到了编译问题,它给出了与标题相同的消息。

此处'然后是代码

#include <stdio.h>
#include <math.h>
double test(double x){
    return pow(x,2);
}

double tarpez(double a,double b, int n, double (*test)(double )){
    /*x[n] represents the parameter
      y[n] respresents the parameter ,a initial x, 
      h is the length of divided spaces*/

    double h,x[n],y[n],So,Se;//So-- sum of odd, Se- ...even
    h = (b-a)/n;
    if (n%2==1){
        n+=1;
    }
    for (int i ; i<=n;i++){
        x[i]= a+i*h;
        y[i] = test(x[i]);
        printf("%lf\n",y[i] );

    }   
}

int main(void){
    double x,a,b,fn;
    int n;
    fn =
        tarpez(a,b,n,test(x));
}

1 个答案:

答案 0 :(得分:0)

Primo,double(* test)(double)是类型名,如int或float。所以你需要声明double(* test)(double)foo,其中foo是变量名(如int n)。 Sceundo,类型名称不能是功能名称。函数指针需要返回tupe,args类型和无保留名称。所以尝试double(* custom_name)(double)。 Tertio,主要功能没有tarpez(a,b,n,test(x))。使用tarpez(a,b,n,test);您只在此空间中使用名称。

PS 尝试使用typedef。