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

标签 c pointers structure

好吧,我正在尝试编写一个程序,使用辛普森 3/8 规则对积分进行数值计算。我在将值从 Integral *newintegral 传递到 simpson() 函数时遇到问题。我对自己对结构和指针的理解不是很有信心,而且我整天都在复习讲义并上网查资料,但我仍然不明白为什么它不起作用。

当我尝试构建我的程序时,它出现了许多错误,特别是:第 46 行“积分之前的预期表达式”和第 55-63 行的大部分“'->'参数类型无效” (有“积分”)我不明白为什么会发生第一个,因为我的所有讲师都举了这种类型的例子,当将结构传递给函数时,只有语法 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;
}'

最佳答案

您当前正在将指针传递给采用单个 Integral 参数的函数。

你的原型(prototype),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(Integral i); 将不起作用,因为您正在重新声明函数参数的类型。编译器将声明:

:46:13: error: expected expression before ‘Integral’

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

解决方案:

按如下方式更改函数原型(prototype):

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

关于c - 将结构(指向结构的指针?)传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20600823/

相关文章:

c - 哪些 zlib 函数与 WinZip 兼容?

c - 宏定义中的奇怪语法 [C]

C++ 类和 Xcode

包含函数指针的 c 结构

C和动态结构元素访问

c - C编译器如何处理不同的主函数定义?

python - 将 Cython 中的 numpy 数组传递给需要动态分配数组的 C 函数

按引用调用函数

c - 在 C 中使用指针扫描字符串

c - 使用结构体成员定义变量