C语言 数组下标不是整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4635142/
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
提问by Dimitri
following this previous question Malloc Memory Corruption in C, now i have another problem. I have the same code. Now I am trying to multiply the values contained in the arrays A * vc and store in res. Then A is set to zero and i do a second multiplication with res and vc and i store the values in A. (A and Q are square matrices and mc and vc are N lines two columns matrices or arrays). Here is my code :
继上一个问题Malloc Memory Corruption in C 之后,现在我有另一个问题。我有相同的代码。现在我试图将数组 A * vc 中包含的值相乘并存储在 res 中。然后 A 设置为零,我与 res 和 vc 进行第二次乘法,并将值存储在 A 中。(A 和 Q 是方阵,mc 和 vc 是 N 行两列矩阵或数组)。这是我的代码:
int jacobi_gpu(double A[], double Q[],
double tol, long int dim){
int nrot, p, q, k, tid;
double c, s;
double *mc, *vc, *res;
int i,kc;
double vc1, vc2;
mc = (double *)malloc(2 * dim * sizeof(double));
vc = (double *)malloc(2 * dim * sizeof(double));
vc = (double *)malloc(dim * dim * sizeof(double));
if( mc == NULL || vc == NULL){
fprintf(stderr, "pb allocation matricre\n");
exit(1);
}
nrot = 0;
for(k = 0; k < dim - 1; k++){
eye(mc, dim);
eye(vc, dim);
for(tid = 0; tid < floor(dim /2); tid++){
p = (tid + k)%(dim - 1);
if(tid != 0)
q = (dim - tid + k - 1)%(dim - 1);
else
q = dim - 1;
printf("p = %d | q = %d\n", p, q);
if(fabs(A[p + q*dim]) > tol){
nrot++;
symschur2(A, dim, p, q, &c, &s);
mc[2*tid] = p; vc[2 * tid] = c;
mc[2*tid + 1] = q; vc[2*tid + 1] = -s;
mc[2*tid + 2*(dim - 2*tid) - 2] = p; vc[2*tid + 2*(dim - 2*tid) - 2 ] = s;
mc[2*tid + 2*(dim - 2*tid) - 1] = q; vc[2 * tid + 2*(dim - 2*tid) - 1 ] = c;
}
}
for( i = 0; i< dim; i++){
for(kc=0; kc < dim; kc++){
if( kc < floor(dim/2)) {
vc1 = vc[2*kc + i*dim];
vc2 = vc[2*kc + 2*(dim - 2*kc) - 2];
}else {
vc1 = vc[2*kc+1 + i*dim];
vc2 = vc[2*kc - 2*(dim - 2*kc) - 1];
}
res[kc + i*dim] = A[mc[2*kc] + i*dim]*vc1 + A[mc[2*kc + 1] + i*dim]*vc2;
}
}
zero(A, dim);
for( i = 0; i< dim; i++){
for(kc=0; kc < dim; k++){
if( k < floor(dim/2)){
vc1 = vc[2*kc + i*dim];
vc2 = vc[2*kc + 2*(dim - 2*kc) - 2];
}else {
vc1 = vc[2*kc+1 + i*dim];
vc2 = vc[2*kc - 2*(dim - 2*kc) - 1];
}
A[kc + i*dim] = res[mc[2*kc] + i*dim]*vc1 + res[mc[2*kc + 1] + i*dim]*vc2;
}
}
affiche(mc,dim,2,"Matrice creuse");
affiche(vc,dim,2,"Valeur creuse");
}
free(mc);
free(vc);
free(res);
return nrot;
}
When i try to compile, i have this error :
当我尝试编译时,出现此错误:
jacobi_gpu.c: In function ‘jacobi_gpu':
jacobi_gpu.c:103: error: array subscript is not an integer
jacobi_gpu.c:103: error: array subscript is not an integer
jacobi_gpu.c:118: error: array subscript is not an integer
jacobi_gpu.c:118: error: array subscript is not an integer
make: *** [jacobi_gpu.o] Erreur 1
The corresponding lines are where I store the results in res and A :
相应的行是我将结果存储在 res 和 A 的地方:
res[kc + i*dim] = A[mc[2*kc] + i*dim]*vc1 + A[mc[2*kc + 1] + i*dim]*vc2;
and
和
A[kc + i*dim] = res[mc[2*kc] + i*dim]*vc1 + res[mc[2*kc + 1] + i*dim]*vc2;
Can someone explain me what is this error and how can i correct it? Thanks for your help. ;)
有人可以解释一下这个错误是什么,我该如何纠正它?谢谢你的帮助。;)
回答by Anycorn
mcis of type double. It has to be integral type
mc是双精度型。它必须是整数类型
回答by Sarwar Erfan
mc is pointer to double.
mc 是指向 double 的指针。
A[mc[2*kc + 1]
In above, you are indexing A with a value in mc (double array). And, there are other similar cases. If you are sure of the values, cast to int
在上面,您使用 mc (双数组)中的值对 A 进行索引。而且,还有其他类似的案例。如果您确定这些值,请强制转换为 int
回答by chrisaycock
Your declaration of mc:
您的声明mc:
mc = (double *)malloc(2 * dim * sizeof(double));
And then you use mcmultiple times in your array access. For example:
然后您mc在数组访问中多次使用。例如:
A[mc[2*kc + 1] ...]
Can you change mcto be an intarray instead of a double?
你能改成mc一个int数组而不是一个double吗?
回答by Sean
Looks like you're using entries in mc, which are doubles, as a part of array subscripts, thus making the entire subscript a double.
看起来您正在使用 中的条目mc,这是双打,作为数组下标的一部分,从而使整个下标成为双打。
If you meant to do this, try casting back to an integer. I don't know what the context of this problem is, but I'd take a real good look at what you're doing to ensure you really want to use the contents of mcas a subscript.
如果您打算这样做,请尝试转换回整数。我不知道这个问题的上下文是什么,但我会好好看看你在做什么,以确保你真的想使用 的内容mc作为下标。
回答by Charles Salvia
The compiler is complaining because the expression you use as an array index evaluates to type double.
编译器抱怨是因为您用作数组索引的表达式的计算结果为 type double。
In other words, the expression:
换句话说,表达式:
mc[2*kc] + i*dim
...will give you a result which is of type double. You may want to look into the rules for usual arithmetic type conversionsin C if you don't understand whythis expression evaluates to a double.
...会给你一个类型的结果double。如果您不明白为什么此表达式的计算结果为 a ,您可能需要查看C 中常用算术类型转换的规则。double
The problem is that array indices must be integral types, like intor long. This is because the array subscript operator in C is basically shorthand for pointer arithmetic. In other words, saying array[N]is the same as saying *(array + N). But you can't do pointer arithmetic with non-integral types like floator double, so of course the array subscript operator won't work that way either.
问题是数组索引必须是整数类型,例如intor long。这是因为 C 中的数组下标运算符基本上是指针运算的简写。换句话说, sayarray[N]与 say 相同*(array + N)。但是你不能对像floatordouble这样的非整数类型进行指针运算,所以当然数组下标运算符也不会那样工作。
To fix this, you'll need to cast the result of your array-indexing expression to an integral type.
要解决此问题,您需要将数组索引表达式的结果转换为整数类型。
回答by Null Set
mc is an array of doubles, and floating point values cannot be used to index arrays. I notice that nowhere in your code do you assign anything other than integers to mc. You should consider changing mc's type to an array of integers.
mc 是一个双精度数组,浮点值不能用于索引数组。我注意到在您的代码中,除了整数之外,您没有为 mc 分配任何其他内容。您应该考虑将 mc 的类型更改为整数数组。

