【问题标题】:Passing a structure (pointer to structure?) to a function将结构(指向结构的指针?)传递给函数
【发布时间】:2013-12-15 22:43:16
【问题描述】:

好吧,我正在尝试编写一个程序,该程序使用辛普森的 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;
}'

【问题讨论】:

    标签: c pointers structure


    【解决方案1】:

    您当前正在传递一个指向函数的指针,该函数采用单个 Integral 参数。

    您的原型 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-&gt;),但是您的函数原型告诉编译器您将整个 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

    【讨论】:

      【解决方案2】:

      两个明显的问题:-

      Line 46 : simpson(Integral i);
      

      ...应该只是simpson(i);。放一个类型就是一个错误。

      还有这个,稍后:

      double simpson(Integral i) 
      

      .. 告诉编译器传入 Integral object 但您使用间接运算符,即 i-&gt;limits ,就好像您被传递了一个指针一样。最简单的解决方法是让函数期待一个指针,如下所示:

      double simpson(Integral *i) 
      

      【讨论】:

        猜你喜欢
        • 2021-07-17
        • 2020-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多