7

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;
}
toriningen
  • 7,196
  • 3
  • 46
  • 68
Matias Rodriguez
  • 81
  • 1
  • 1
  • 3
  • 3
    The error message seems clear enough. 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
  • 1
    @KeithThompson, I've never seen the 5[array] syntax. I tried it and it seems the same as array[5], but where/why would one use something like that? Thanks. – Charlie Burns Nov 27 '13 at 19:06
  • 1
    @CharlieBurns; `array[5]` is equivalent to `*(array + 5)` and same for `5[array` which is equal to `*(5 + array)`. That's why `array[5] = 5[array]`. *where/why would one use something like that?*: I think it is used only for **obfuscation** :) – haccks Nov 27 '13 at 19:09
  • 1
    added [code] int *q = dos; [/code]and changed this [code] total += (datos [p]-avg) * (data [p +1]-avg);} so {total + = (data [q]-prom ) * (data [q +1]-avg);} but the error continues. Specifically the error is shown in this line {total + = (data [p]-avg) * (data [p +1]-avg);} – Matias Rodriguez Nov 27 '13 at 19:09
  • 2
    @CharlieBurns: Hopefully you would never use it. It works because `x[y]` is by definition equivalent to `*(x+y)`, and addition is commutative (even for pointer+integer). See [this question](http://stackoverflow.com/q/381542/827263); the accepted answer is massively upvoted, but [my answer](http://stackoverflow.com/a/18393343/827263) goes into the history. – Keith Thompson Nov 27 '13 at 19:10
  • Thanks for the history. Very interesting. – Charlie Burns Nov 27 '13 at 19:17
  • @modchan Why did you change the question into something totally different? All the answers look like complete gibberish now. – Mr Lister Nov 27 '13 at 19:54
  • @MrLister, well, because 'data' and 'average' makes more sense for non-Spanish speakers. However, changed identifiers back (peer review pending), so answers are not so gibberish now. – toriningen Nov 27 '13 at 21:48

4 Answers4

8

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.

yamafontes
  • 5,552
  • 1
  • 18
  • 18
  • aded `int *q = datos` and changed `total += (datos[p]-prom)*(datos[p+1]-prom);` for `total += (datos[q]-prom)*(datos[q+1]-prom);` but the error still appears and specifically on the line `total += (datos[q]-prom)*(datos[q+1]-prom);` – Matias Rodriguez Nov 27 '13 at 19:36
  • `q` is not an `int` -- it is an `int*`. Once again, you can only have `int` values as array subscripts. If you want to use `q` as your index, you will need to dereference it first with `*q`. – yamafontes Nov 27 '13 at 19:54
  • What can I do, when I need do this: `double array[1000000000]; for(double i = 0 ; i < n; i++) array[i] = i;` So, int dont have number as 1000000000. How can I iterate throught this array? – Ady96 Oct 25 '18 at 12:35
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.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    aded `int *q = datos` and changed `total += (datos[p]-prom)*(datos[p+1]-prom);` for `total += (datos[q]-prom)*(datos[q+1]-prom);` but the error still appears and specifically on the line `total += (datos[q]-prom)*(datos[q+1]-prom);` – Matias Rodriguez Nov 27 '13 at 19:39
2

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.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
2

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
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256