错误:无法将类型为“double *”的值分配给“double”类型的实体

时间:2014-03-02 15:57:15

标签: c++

我输入代码时遇到问题。

...
double* FindMax(const double* const arr, int n)
{
    double max;
    ...
    return &max;
}

int main()
{
    ...
    maxVal = FindMax(value, numbers);
    ...
}

当我调用函数FindMax时,程序错误并且不允许我将值传递给函数FindMax。 我怎么能让它成为可能,非常感谢!

1 个答案:

答案 0 :(得分:5)

如果maxVal为两倍,则无法指定&max的值。它应该是double *  其次,永远不要返回局部变量的地址。因为一旦从函数返回,局部变量就会死掉。

相关问题