【问题标题】:expected 'double *' but argument is of type 'double' and incompatible type for argument 2/3/4/5 of function预期为“双 *”,但参数是“双”类型,并且函数的参数 2/3/4/5 的类型不兼容
【发布时间】:2018-11-09 20:49:55
【问题描述】:

我乞求你们的帮助,我不是最好的程序员,我花了很多时间在这上面,我很累很沮丧:[

基本上,我想将int 和 4 个单维数组传递给一个函数,该函数将返回一个指向数组(用于我的目的的解决方案向量)的指针,我在 C 中理解该指针,我可以通过静态命令在函数中定义.

基本上它说它不明白为什么将 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;
}

【问题讨论】:

  • p=thomas_algorithm(n, c[N-1], b[N], a[N-1], d[N]); -> p=thomas_algorithm(n, c, b, a, d);a[i] 是特定元素,而 a 是数组本身(衰减为指针)。
  • @Osiris 没错!写一个答案!

标签: c++ c c89 numerical-analysis


【解决方案1】:

你的函数调用是错误的。

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

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

正确的函数调用应该是:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-22
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 2018-09-07
    • 1970-01-01
    相关资源
    最近更新 更多