Hi,
Can someone tell me how to fix the function below. I am getting this compile error regarding the last line:
cannot convert `const double' to `const double*' in return
What should I return according to the function parameters I've written?

Thanks.

const double * maximum(const double a[], int size)
{
    if (size == 0) return NULL;
    const double *highest = a;
    for (int i = 0; i < size; i++)
        if (a[i] > *highest)
        {
            *highest == a[i];
        }
    return *highest;

Recommended Answers

All 2 Replies

The syntax error being reported means you have to return highest, not the highest derefenced.

The logic error is that you need to have the ability to change the value of highest so don't try to force highest to be a const double in the first place.

hi
There are two things first

const double *highest = a;

this line says highest is a pointer to a costant means u cant change the content highest is pointing to, next

if (a[i] > *highest)
        {
            *highest == a[i]; // "==" ??
        }

removing that if u do it like

if (a[i] > *highest)
        {
            *highest = a[i];
        }

u will be trying to change a contant, which is illegal.

better if u write

const double * maximum(const double a[], int size)
{
    if (size == 0) return NULL;
    double *highest = (double*)a;
    for (int i = 0; i < size; i++)
        if (a[i] > *highest)
        {
            *highest = a[i];
        }
    return highest;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.