Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Array subscript is not an integer" in C program

Tags:

c

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;
}
like image 203
Matias Rodriguez Avatar asked Nov 27 '13 18:11

Matias Rodriguez


People also ask

Is the subscript in an array an integer?

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.

Can I use a float as an array subscript in C?

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.

Can we use INTs as array indices in C?

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 .

How to subscript an array in Datos[P]?

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.


2 Answers

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.

like image 130
yamafontes Avatar answered Oct 14 '22 06:10

yamafontes


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.

like image 3
haccks Avatar answered Oct 14 '22 05:10

haccks