Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 259 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Array subscript is not an integer" in C program

#1
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;
}
Reply

#2
`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.
Reply

#3
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.
Reply

#4
You can use an int:

int p = 0;
...
while (p<n) {
// rest is the same

or, you can use `p` as a pointer directly, which is, I suppose, what you intended.

while(p<datos+n) {
total += (*p-prom)*(*(p+1)-prom);

Note, however, that you never increment `p`, so the loop will never end. Also, you never initialise `total`, so the result will be a random garbage value.
Reply

#5
C11dr 6.5.2.1 **Array subscripting** ... "One of the expressions shall have type ‘‘pointer to complete object _type_’’, the other expression shall have integer type, and the result has type ‘‘_type_’’." ...

With `[]`, you get to do:

// pointer_type[integer_type]
float data[];
int n;
float total;
total = data[n];

// or the unconventional equivalent
// integer_type[pointer_type]
total = n[data]; // But let's leave that for another post

// Instead the OP did
// pointer_type[pointer_type]
// and received error: "Array subscript is not an integer"
float *p;
total = data[p];

The usage of `float` is not the issue here, but the usage of a pointer.

An integer type includes types `int`, `unsigned`, `size_t`, `long`, etc.

---

I think the OP wanted the following (or something like this)

float deviation(float data[], int n) {
if (i <= 0) return 0;
float data_average = average(data, n);
float total = 0.0; // don't forget to set to 0.0
float *p = data;

while (p < (data + n)) {
total += (*p - data_average) * (*p - data_average);
p++;
}
return sqrt(total / n); // div by 0 averted
}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through