2011-09-15 73 views
1

我得到的錯誤C編程:下標值既不是數組,也不指針

下標值既不是數組,也不指針

,當我嘗試編譯我的程序。我知道這與變量沒有被聲明有關,但我檢查了一切,並且它似乎被聲明瞭。

static char getValue(LOCATION l) 
{ 
    /*return carpark[l.col][l.row]; // Assumes that location is valid. Safe code is 
     below: 
    */ 


    if (isValidLocation(l)) { 
     return carpark[l.col][l.row]; <<<<<<<< this line 
     }  // returns char if valid (safe) 
    else { 
     return '.'; 
    } 

對應於這部分代碼在頭

typedef struct 
{ 
    /* Rectangular grid of characters representing the position of 
     all cars in the game. Each car appears precisely once in 
     the carpark */ 
    char grid[MAXCARPARKSIZE][MAXCARPARKSIZE]; 
    /* The number of rows used in carpark */ 
    int nRows; 
    /* The number of columns used in carpark */ 
    int nCols; 
    /* The location of the exit */ 
    LOCATION exit; 
} CARPARK; 

停車場被宣佈在主PROG有:

CARPARK carpark. 

感謝您的幫助。

+0

你什麼錯誤?請在您的問題中粘貼其確切的文字。 – Chriszuma

回答

7

carpark不是一個數組,所以你可能想是這樣的:

return carpark.grid[l.col][l.row]; 
0

錯誤消息告訴您確切問題是什麼。變量carpark既不是數組也不是指針,所以不能將[]運算符應用於它。

carpark.grid,然而,一個數組,所以你可以寫

return carpark.grid[l.col][l.row]; 
相關問題