C 将结构(指向结构的指针?)传递给函数

C 将结构(指向结构的指针?)传递给函数,c,pointers,structure,C,Pointers,Structure,我正在写一个程序,用Simpson的3/8法则数值计算积分。我在将值从Integral*newintegral传递到simpson()函数时遇到问题。我对自己对结构和指针的理解没有太大的信心,我整天都在复习课堂讲稿和在线查看信息,但我仍然不明白为什么它不起作用 在我尝试构建程序时,它出现了一些错误,特别是:在第46行“积分前的预期表达式”和在大多数55-63行“无效类型的'->'参数(具有'Integral'))我不明白为什么会出现第一个例子,因为我的讲师们都举过这种类型的例子,当把一个结构传递

我正在写一个程序,用Simpson的3/8法则数值计算积分。我在将值从Integral*newintegral传递到simpson()函数时遇到问题。我对自己对结构和指针的理解没有太大的信心,我整天都在复习课堂讲稿和在线查看信息,但我仍然不明白为什么它不起作用

在我尝试构建程序时,它出现了一些错误,特别是:在第46行“积分前的预期表达式”和在大多数55-63行“无效类型的'->'参数(具有'Integral'))我不明白为什么会出现第一个例子,因为我的讲师们都举过这种类型的例子,当把一个结构传递给一个函数的时候,只有语法func(Struct_define_name individual_Struct_name)。我想这就是我的想法(Integral是结构类型的名称,I是特定的结构)但显然不是

我认为这两个问题是相互关联的,所以我包括了我所有的上下文代码,但是实际有错误的行是46和55-63,如上所述。我可能一开始就把结构定义错了

(顺便说一句,simpson()函数中的数学运算现在实际上并不正常,但这不是我所关心的)

我也试着看其他类似的问题,但我不明白其他代码在做什么,所以我无法从中推断出如何修复我的代码。我知道这与其他人不太相关,但我对编程的理解还不够透彻,无法尝试在一般意义上表达我的问题

'#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct integral {
    double result, limits[2];
    int degree;
    double coefficients[];
} Integral;

// Prototype of function that calculates integral using Simpson's 3/8 rule
double simpson(Integral i);


// function (?) which initialises structure
Integral *newintegral() {
    Integral *i = malloc(sizeof *i);
    double lim1_in, lim2_in;
    int degree_input, n;

    printf("Please enter the degree of the polynomial.\n");
    scanf("%d", &degree_input);
    i->degree = degree_input;

    printf("Please enter the %d coefficients of the polynomial, starting\n"
           "from the highest power term in the polynomial.\n", (i->degree+1));

        for (n=i->degree+1; n>0; n=n-1) {
            scanf("%lg", &i->coefficients[n-1]);
}

    printf("Please enter the upper limit of the integral.\n");
    scanf("%lg", &lim1_in);
    i->limits[0] = lim1_in;

    printf("Please enter the lower limit of the integral.\n");
    scanf("%lg", &lim2_in);
    i->limits[1] = lim2_in;

    return i;
}


int main() {
    Integral *i = newintegral();
    simpson(Integral i);
    return 0;
}


double simpson(Integral i) {
    int n;
    double term1, term2, term3, term4;

    for (n=(i->degree); n>0; n=n-1) {
        term1=(pow(i->limits[1],n)*(i->coefficients[n]))+term1;
        term2=(pow(((((2*(i->limits[1]))+(i->limits[0])))/3),n)*(i->coefficients[n]))+term2;
        term3=(pow(((((2*(i->limits[0]))+(i->limits[1])))/3),n)*(i->coefficients[n]))+term3;
        term4=(pow(i->limits[0],n)*(i->coefficients[n]))+term4;
    }
    i->result = (((i->limits[0])-(i->limits[1]))/8)*(term1+(3*term2)+(3*term3)+term4);

    printf("The integral is %lg\n", i->result);

    return 0;
}'
”#包括
#包括
#包括
typedef结构积分{
双重结果,限制[2];
智力度;
双系数[];
}积分;
//使用辛普森3/8规则计算积分的函数原型
双辛普森(积分i);
//初始化结构的函数(?)
整型*newintegral(){
积分*i=malloc(sizeof*i);
双lim1_-in,lim2_-in;
int度_输入,n;
printf(“请输入多项式的阶数。\n”);
scanf(“%d”、°ree\u输入);
i->度=度输入;
printf(“请输入多项式的%d系数,开始\n”
“从多项式中的最高幂项开始。\n”,(i->degree+1);
对于(n=i->度+1;n>0;n=n-1){
scanf(“%lg”,&i->系数[n-1]);
}
printf(“请输入积分的上限。\n”);
扫描频率(“%lg”、&lim1_英寸);
i->limits[0]=lim1\u in;
printf(“请输入积分的下限。\n”);
scanf(“%lg”和lim2_in);
i->limits[1]=lim2\u in;
返回i;
}
int main(){
积分*i=newintegral();
辛普森(积分一);
返回0;
}
双辛普森(积分i){
int n;
双条款1、条款2、条款3、条款4;
对于(n=(i->度);n>0;n=n-1){
term1=(功率(i->极限[1],n)*(i->系数[n])+term1;
term2=(pow(((((2*(i->limits[1]))+(i->limits[0]))/3),n)*(i->系数[n])+term2;
term3=(pow(((((2*(i->limits[0]))+(i->limits[1]))/3),n)*(i->系数[n])+term3;
term4=(功率(i->极限[0],n)*(i->系数[n])+term4;
}
i->result=((i->limits[0])-(i->limits[1])/8)*(term1+(3*term2)+(3*term3)+term4);
printf(“积分是%lg\n”,i->result);
返回0;
}'
两个明显的问题:-

Line 46 : simpson(Integral i);
…应该是
simpson(i);
。将类型放在那里只是一个错误

这一点,稍后:

double simpson(Integral i) 
..告诉编译器传递Integralobject,但您使用间接运算符,即
i->limits
,就像传递了一个指针一样。最简单的修复方法是使函数期望一个指针,如下所示:

double simpson(Integral *i) 

您当前正在传递一个指向一个函数的指针,该函数只接受一个
整型
参数

原型,
double-simpson(Integral i);
告诉编译器“声明一个名为
simpson
的函数,该函数返回一个
double
,并获取一个由函数内标识符
i
引用的
Integral

但是,在
main()
中,您会说:

int main() {
    //declare a pointer to an Integral and assign it to the return of 'i'
    Integral *i = newintegral();
    //call the function simpson with i.  
    //However, you are redeclaring the type of the function argument, so the compiler will complain.
    simpson(Integral i);
    return 0;
}
你的电话,
simpson(积分i)将不起作用,因为您正在重新声明函数参数的类型。编译器将声明:

:46:13: error: expected expression before ‘Integral’
您真正需要的是
simpson()
将指向Integral的指针作为其参数。实际上,您已经在函数内部处理了这个问题(使用
i->
),但是您的函数原型告诉编译器您正在将整个
struct Integral
作为函数参数传递

解决方案:

按如下方式更改您的功能原型:

double simpson(Integral *i); // function returning double taking single pointer to an Integral named i.
…并将
main()
更改为如下所示:

int main(void) { //In C main has two valid definitions: 
                 //int main(void), or int main(int argc, char **argv)

    Integral *i = newintegral();
    simpson(i);
    return 0;
}
总之,您对指针的理解是正确的,但不是如何将指针传递给函数

**旁注: 请记住,始终在生成代码时启用所有警告。编译器将为您提供非常有用的诊断,帮助您快速找到此类问题的解决方案。对于GCC,至少使用
GCC-Wall myprogram.c