user2251921
user2251921

Reputation: 127

Cannot convert double to double

void load(int *n, double *x, double **arr, bool randomize)
{
    *arr = (double*)malloc((*n + 1) * sizeof(double));
    srand(time(NULL));
    for(int i = 0; i <= *n; i++)
    {
        if(! randomize)
        {   
            scanf("%lf", *arr + i);
        }
        else
        {
            *(arr + i) = rand();
        }
    }
}

Based on the randomize parameter I want to fill the array with random or custom double numbers. This code however does not compile, it displays "invalid conversion from int to double" in else section.

Replacing rand() with any float value like 5.0 shows "cannot convert double to double" instead.

(double) rand() or (double) 5 throw similar errors.

*n of course is read earlier in this function, I just cut it off here.

What can be wrong here?

Upvotes: 4

Views: 1083

Answers (2)

BLUEPIXY
BLUEPIXY

Reputation: 40145

*(arr + i) = rand(); -> (*arr)[i] = rand();

Upvotes: 0

NPE
NPE

Reputation: 500217

The error message you are quoting is incomplete. It's missing an asterisk after double: the type of *(arr + i) is double* rather than double, and you can't convert int or double to double*.

Presumably you mean

        *(*arr + i) = rand();

or

        (*arr)[i] = rand();

Upvotes: 1

Related Questions