Netanel
Netanel

Reputation: 11

I get the following error: "Array subscript is not an integer". but the array is of type int. what do I do wrong?

The deviation function throws me the following error: "Array subscript is not an integer". If you can help me locate the cause of error I'll appreciate it.

    #ifdef _MSC_VER
    #define _CRT_SECURE_NO_WARNINGS
    #endif
    #include "stdio.h"
    #include "stdlib.h"
    #include <stdbool.h>

    //----<< To store matrix cell cordinates >>-------------------------- 
    struct point
    {
      double x;
      double y;
    };
    typedef struct point Point;

    //---<< A Data Structure for queue used in robot_maze  >>---------------------
    struct queueNode 
    { 
        Point pt;  // The cordinates of a cell 
        int dist;  // cell's distance of from the source 
    };

    //----<< check whether given cell (row, col) is a valid cell or not >>--------
    bool isValid(int row, int col, int ROW, int COL) 
    { 
        // return true if row number and column number is in range 
        return (row >= 0) && (row < ROW) && (col >= 0) && (col < COL); 
    } 

    //----------------------------------------------------------------------------
    int robot_maze()
    {
        int solution = -1;
        int ROW, COL, i,j;
        Point src, dest;
        scanf("%d", &ROW);
        scanf("%d", &COL);
        scanf("%d %d",&src.x,&src.y);
        scanf("%d %d",&dest.x,&dest.y);
        int arr[ROW][COL],visited[ROW][COL];

        for (i = 0;i < ROW;i++)
        for (j = 0;j < COL;j++)
        {
            scanf("%d", &arr[i][j]);
            visited[i][j] = -1;
        };

        // check both source and destination cell of the matrix have value 0 
        //if (arr[src.x][src.y] || arr[dest.x][dest.y]) 
        //    return -1; 

        return solution;
    }

The "if" statement (behind the //) should just work and enter if both values are 0. notice that I defined the 2D matrix "arr" as type int. why do I get this error?

the problem I try to solve is the "Shortest path in a Binary Maze" problem, but I got stuck at the beginning.

Upvotes: 0

Views: 438

Answers (1)

Susmit Agrawal
Susmit Agrawal

Reputation: 3764

Read the error carefully: It states Array subscript is not an integer. The error in question is because of the values used to access the array's elements.

src.x, src.y, dest.x and dest.y are of type double. You need to redefine point as:

struct point
{
  int x;
  int y;
};

Or, if you have to use double, you can cast to int.

Upvotes: 1

Related Questions