Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: invalid type argument of unary '*'

I don't understand these errors can someone explain?

error: invalid type argument of unary '' (have 'double') error: invalid type argument of unary '' (have 'double') error: invalid type argument of unary '*' (have 'double')

    double getMedian(double *array, int *hours){
    if (*hours <= 0) return 0;
    if (*hours % 2) return (float)*array[(*hours + 1) / 2];
    else{int pos = *hours / 2;
    return (float)(*array[pos] + *array[pos + 1]) / 2;}}
like image 468
user3502479 Avatar asked Apr 07 '14 03:04

user3502479


2 Answers

You are already dereferencing array with the [] operator. What you want is:

double getMedian(double *array, int *hours){
if (*hours <= 0) return 0;
if (*hours % 2) return (float)array[(*hours + 1) / 2];
else{int pos = *hours / 2;
return (float)(array[pos] + array[pos + 1]) / 2;}}

Note that writing x[y] is shorthand for *(x + (y)). In your code, you have essentially have the equivalent of **array.

like image 133
ppl Avatar answered Oct 08 '22 08:10

ppl


When you use the [] operator on the arrays or pointers, you don't have to dereference them again to get the value. you could just say,

if (*hours % 2) return (float)array[(*hours + 1) / 2];

and

return (float)(array[pos] + (array[pos + 1]) / 2);
like image 23
sajas Avatar answered Oct 08 '22 09:10

sajas