好的,我正在尝试编写一个使用Simpson的3/8规则对整数进行数值计算的程序。我在将值从Integral * newintegral传递到simpson()函数时遇到问题。我对结构和指针的理解并不十分自信,而且我整天都在阅读讲义并在线检查信息,但我仍然不明白为什么它不起作用。
目前,当我尝试构建程序时,它会出现许多错误,尤其是:在第46行“ Integral之前的预期表达式”和在55-63行中的大多数“->”(具有'积分”)我不明白为什么会发生第一个事件,因为我的所有讲师都将这种类型的例子传递给函数时,语法仅为func(Struct_define_name personal_struct_name)。我认为这就是我在做的事情记住(整数是结构类型的名称,而我是特定结构),但显然不是。
我认为这两个问题是联系在一起的,因此我将所有代码都包含在内,但是实际上有错误的行如上所述,分别是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", °ree_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
参数的函数的指针。
您的原型double simpson(Integral i);
告诉编译器“声明一个函数simpson
,该函数返回adouble
并接受该函数内部Integral
标识符引用的单个i
函数。
但是,在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()
采取一个指针,整体作为其参数。您实际上已经在函数内部处理了这个问题(使用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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句