错误:二进制 * 的操作数无效(具有 'double' 和"双 *")



我在这个代码中有一个错误:

mat mat_y(mat b, mat l, int n) {
mat c = c = mat_new(n);
for(i=0; i<n; i++)
{
c[i]=b[i];
for(j=0; j<i; j++)
{
c[i] -= l[i][j] * c[j];
}
}
return c;
}

错误:

二进制 * 的操作数无效(具有"双精度"和"双精度 *"(

以下是一些信息:

typedef double **mat;
int i,j,k;
void mat_zero(mat x, int n) {
for (i = 0; i < n; i++) 
for (j = 0; j < n; j++)
x[i][j] = 0;
}

mat mat_new(int n) {
mat x = malloc(sizeof(double*) * n);
x[0] = malloc(sizeof(double) * n * n);
for (i = 0; i < n; i++)
x[i] = x[0] + n * i;
mat_zero(x, n);
return x;
}

二元运算符*不能将指针和双精度相乘。从您的代码中可以看出,尽管您实际上并没有尝试乘以指针,而是错误地访问了双指针。

c[i] -= l[i][j] * c[j];

所以对于像double**这样的指针,它意味着指向指针的指针。如果你只访问它一次,比如c[i],这会取消引用外部指针,从而产生指向双精度的指针。您需要取消引用double**两次才能访问其中的浮点数。

相关内容

最新更新