简体   繁体   中英

error: invalid operands to binary * (have 'double' and 'double *')

I have an error in this code:

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;
}

The error:

invalid operands to binary * (have 'double' and 'double *')

Here are some information:

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;
}

The binary operator * cannot multiply a pointer and a double. It appears from your code though you are not actually trying to multiply a pointer but are incorrectly accessing a double pointer.

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

So for pointers like double** , it means pointer to a pointer . If you only access it once, like c[i] , this dereferences the outer pointer which results in a pointer to a double. You need to dereference a double** twice in order to access the floating point number in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM