繁体   English   中英

此数组错误是什么意思?

[英]What does this array error mean?

void arrayRound(int id, double baln)
{
    baln[id] = (baln[id]*100) + 0.5;
    int temp = (int) baln[id];
    baln[id] = (double) temp;
    baln[id] = baln[id] / 100;
}

函数主体是给我错误消息的原因。 该函数用于将数组索引四舍五入到最接近的百分之一。 我分别将index变量和数组传递给了函数。 这是错误消息:

Fxns.c:70: error: subscripted value is neither array nor pointer
Fxns.c:70: error: subscripted value is neither array nor pointer
Fxns.c:71: error: subscripted value is neither array nor pointer
Fxns.c:72: error: subscripted value is neither array nor pointer
Fxns.c:73: error: subscripted value is neither array nor pointer
Fxns.c:73: error: subscripted value is neither array nor pointer

我的第一个猜测是,我需要在参数字段的栏杆后添加空括号,但这无济于事。 有任何想法吗?

您正在尝试治疗baln类型的double像一个数组(使用索引)。这是行不通的。

您的参数应声明为

double *baln

指向double的指针,或作为double baln[]的指针,其读取方式类似于double的数组,但作为函数参数也表示一个指针。

void arrayRound(int id, double *baln)
{
    baln[id] = (baln[id]*100) + 0.5;
    int temp = (int) baln[id];
    baln[id] = (double) temp;
    baln[id] = baln[id] / 100;
}

会编译,但是由于您不知道baln指向的内存块大小,因此如果您不小心,可以使用此函数访问未分配的内存。

你说对了; 您确实需要在参数列表中的baln之后包括空括号,如下所示:

void arrayRound(int id, double baln[]);

这是完整的演示。

错误:下标值既不是数组也不是指针

baln[id]

下标价值=谷仓

运算符[]只能在数组或指针上使用。 在您的情况下, baln都不是。 它的类型为double但不是double[]double*

int a[] = { 1,2,3,4,5 };
a[0] = 10;  // Notice the use of `[]`.This is valid because `a` is an array type.

int b = 10;
int * ptr = &b;
ptr[0] = 99;   // Valid because ptr is a pointer type but cause undefined
               // behaviour for any other index in this example.

*ptr = 99 ; // This is more readable than the earlier case.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM