无效的二进制操作数,缺少理论

Invalid binary operands, missing theory

我收到错误消息“二进制 * 的无效操作数(有‘double’和‘double *’)” 尽管 x,y 的变量是正常的双精度变量,但我必须使用用指针编写的函数,所以我发送了它们的地址以使函数工作。我不明白为什么坡度没有错误但只有 *y_intercept 。

void determine_line(double *, double *, double *, double *, double *, double *);

int main()
{
    double x1 = 0.0, x2 = 0.0, y1 = 0.0, y2 = 0.0, m = 0.0, b = 0.0;

    x1 = readValue("x1");
    y1 = readValue("y1");
    x2 = readValue("x2");
    y2 = readValue("y2");

    double *y_intercept = &b;
    double *slope = &m;

    determine_line(&x1,&y1,&x2,&y2,slope,y_intercept);
    printf("\nThe points (%.2lf, %.2lf) and (%.2lf, %.2lf) are on the"
        " line: y = %.2lfx + %.2lf\n",x1,y1,x2,y2,*slope,*y_intercept);
}
void determine_line(double *x1, double *y1, double *x2, double *y2
, double *slope, double *y_intercept)
{
    *slope = (y1-y2)/(x1-x2);
    *y_intercept = y2 - (*slope) * x2 ; // error
}

determine_line的所有参数都是指针。您需要取消引用指针以获取可以对其执行算术运算的数字。

您不会得到 *slope 赋值的错误,因为允许指针减法,尽管在这种情况下结果是未定义的,因为指针不指向相同的对象。但是该行也需要取消引用才能产生正确的结果。

void determine_line(double *x1, double *y1, double *x2, double *y2
, double *slope, double *y_intercept)
{
    *slope = (*y1-*y2)/(*x1-*x2);
    *y_intercept = *y2 - (*slope) * *x2 ;
}

虽然不清楚为什么前 4 个参数首先是指针。唯一需要作为指针的参数是 slopeintercept,因为它们用于返回结果。