Terrence
Terrence

Reputation: 11

C programming: subscripted value is neither array nor pointer

I get the error

subscripted value is neither array nor pointer

when I try to compile my program. I understand it has something to do with the variable not being declared but I checked everything and it seemed to be declared.

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 '.';
    }

Which corresponds to this part of the code in the header

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;

Carpark was declared in the main prog with:

CARPARK carpark. 

Thanks for the help.

Upvotes: 1

Views: 2120

Answers (2)

John Bode
John Bode

Reputation: 123448

The error message is telling you exactly what the problem is. The variable carpark is neither an array nor a pointer, so you cannot apply the [] operator to it.

carpark.grid, however, is an array, so you could write

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

Upvotes: 0

cnicutar
cnicutar

Reputation: 182619

carpark is not an array so you probably want something like:

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

Upvotes: 7

Related Questions