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;
}
8
  • 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.) Commented Nov 27, 2013 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. Commented Nov 27, 2013 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 :) Commented Nov 27, 2013 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);} Commented Nov 27, 2013 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; the accepted answer is massively upvoted, but my answer goes into the history. Commented Nov 27, 2013 at 19:10

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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);
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.
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?
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.

1 Comment

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);
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.

Comments

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.