Unable to 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 arbitrary double numbers. However, this code does not compile; it displays "invalid conversion from int to double" in another section.

Replacing rand() with any floating point value, such as 5.0, means that it is not possible to convert double to double instead.

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

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

What could be wrong here?

+4
source share
2 answers

The error message you are quoting is incomplete. It does not have an asterisk after double : type *(arr + i) is double* , not double , and you cannot convert int or double to double* .

Presumably you mean

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

or

  (*arr)[i] = rand(); 
+2
source

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

0
source

All Articles