C语言 C程序中的“数组下标不是整数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20250616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Array subscript is not an integer" in C program
提问by Matias Rodriguez
The deviationfunction 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.
该deviation函数向我抛出以下错误:"Array subscript is not an integer". 如果您能帮我找到错误的原因,我将不胜感激。
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;
}
回答by yamafontes
pis a pointer to a float. That's why you're getting the error.
p是一个指向浮点数的指针。这就是您收到错误的原因。
float *p, total;
...
total += (datos[p]-prom)*(datos[p+1]-prom);
You can only use intsas array indices in C.
您只能ints在 C 中用作数组索引。
回答by haccks
Array subscripts must be an integer, an inttype. You can't use any other type as array subscript. pis declared as float *p, i.e, a pointer to float. You can't use a pointer as array indices.
数组下标必须是整数,一种int类型。您不能使用任何其他类型作为数组下标。p被声明为float *p,即指向 的指针float。您不能使用指针作为数组索引。
回答by Mr Lister
You can use an int:
您可以使用整数:
int p = 0;
...
while (p<n) {
// rest is the same
or, you can use pas a pointer directly, which is, I suppose, what you intended.
或者,您可以p直接用作指针,我想这就是您的意图。
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.
但是请注意,您永远不会增加p,因此循环永远不会结束。此外,您永远不会初始化total,因此结果将是一个随机的垃圾值。
回答by chux - Reinstate Monica
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''." ...
C11dr 6.5.2.1数组下标......“其中一个表达式的类型应为'指向完整对象类型' '的指针,另一个表达式应为整数类型,结果类型为''类型''。” ...
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 floatis not the issue here, but the usage of a pointer.
的用法float不是这里的问题,而是指针的用法。
An integer type includes types int, unsigned, size_t, long, etc.
的整数类型包括类型int,unsigned,size_t,long,等。
I think the OP wanted the following (or something like this)
我认为 OP 想要以下(或类似的东西)
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
}

