预期为'double *',但参数的类型为'double',且函数的参数2/3/4/5的类型不兼容

时间:2018-11-09 20:49:56

标签: c++ c c89 numerical-analysis

我求你帮我,我不是最好的程序员,我花了很多时间,我很累和沮丧:[

基本上,我想将int和4个单维数组传递给一个函数,该数组将返回一个指向数组的指针(出于我的目的,为解决方案向量),我在CI中可以理解,该指针可以通过静态订购。

基本上,它说它不理解为什么将double类型数组传递给一个返回double指针的函数,并且由于某种原因,它希望指针也是我所理解的参数。 我认为它拖到了粗体字,因为存在兼容性问题,至少我认为这就是该错误的原因。 请帮助我:]

#include <stdio.h>
#define N 4

double* thomas_algorithm(int n, double c[], double b[], double a[], double 
d[]);

int main()
{

int i, n;
double* p;
double a[N-1]={0}, b[N]={0}, c[N-1]={0}, d[N]={0};

printf("please enter the order of the coefficient matrix:\n");

scanf("%d", &n);

printf("please insert the vector c:\n");

for(i=0; i<N-1; i++)
{
    scanf("%lf", &c[i]);
}

printf("please insert the vector b:\n");

for(i=0; i<N; i++)
{
    scanf("%lf", &b[i]);
}

printf("please insert the vector a:\n");

for(i=0; i<N-1; i++)
{
    scanf("%lf", &a[i]);
}

printf("please insert the vector d:\n");

for(i=0; i<N; i++)
{
    scanf("%lf", &d[i]);
}

**p=thomas_algorithm(n, c[N-1], b[N], a[N-1], d[N]);**

for(i=0; i<N; i++)
{
    printf("x(%d)=%f", i+1, p+i);
}

return 0;
}

double* thomas_algorithm(int n, double c[], double b[], double a[], double 
d[]) {

int i;
static double x[N]={0};

for(i=1; i<n-1; i++) /*factorization phase*/
{
    b[i]=b[i]-(a[i]/b[i-1])*c[i-1];
    d[i]=d[i]-(a[i]/b[i-1])*d[i-1];

}
/*backward substitution*/

x[N-1]=d[N-1]/b[N-1];

for(i=n-2; i>-1; i++)
{
    x[i]=(d[i]-c[i]*x[i+1])/b[i];
}
    return x;
}

1 个答案:

答案 0 :(得分:3)

您的函数调用错误。

如果您有一个数组int a[N];和一个函数void func(int a[]),则需要像func(a);这样调用该函数。

在调用中,您传递了数组a[N]的第N个元素,因此编译错误,因为它的类型为double而不是double *。 (这也是无界访问)

正确的函数调用为:

p=thomas_algorithm(n, c, b, a, d);