The deviation
function throws me the following error: "Array subscript is not an integer"
. If you can help me locate the cause of error I'll appreciate it.
float average(float data[], int n) {
float total = 0;
float *p = data;
while (p < (data + n)) {
total = total + *p;
p++;
}
return total / n;
}
float deviation(float data[], int n) {
float data_average = average(data, n);
float total;
float *p = data;
while (p < (data + n)) {
total += (data[p] - data_average) * (data[p + 1] - data_average);
}
return total / 2;
}
main.c:38:20: error: array subscript is not an integer The thing is that A is in fact an int, and also date array.
4 Answers#N#4. p is a pointer to a float. That's why you're getting the error. You can only use ints as array indices in C. Array subscripts must be an integer, an int type. You can't use any other type as array subscript. p is declared as float *p, i.e, a pointer to float. You can't use a pointer as array indices.
You can only use ints as array indices in C. Array subscripts must be an integer, an int type. You can’t use any other type as array subscript. p is declared as float *p , i.e, a pointer to float .
An array subscript has to be an integer. In datos[p], p is a pointer, not an integer. You can't do that. (Well, you can do silly things like 5[array], but you're not doing that here; datos is also a pointer.) – Keith Thompson Nov 27 '13 at 18:52.
p
is a pointer to a float. That's why you're getting the error.
float *p, total;
...
total += (datos[p]-prom)*(datos[p+1]-prom);
You can only use ints
as array indices in C.
Array subscripts must be an integer, an int
type. You can't use any other type as array subscript. p
is declared as float *p
, i.e, a pointer to float
. You can't use a pointer as array indices.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With