簡體   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