0

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.

6
  • 3
    There are two problems: 1. you didn't read the compiler warnings. 2. Microsoft C doesn't have VLAs, therefore in int arr[ROW][COL] and in visited[ROW][COL], ROW and COL need to be constants. Point 2 may not apply on your platform though, you didn't mention what compiler you use. If you don't get any compiler warnings, compile with -Wall Commented May 20, 2019 at 15:31
  • 1
    Aside: you would normally #include <stdio.h> etc unless you are using local versions. Commented May 20, 2019 at 15:33
  • I did not get any warnings. I am using Dev-C++5.11. about the #include "stdio.h", my teacher wrote the libraries as a template for all the students, so that's why I use it. should I use malloc to construct 2D array that is depended by the input by the user? Commented May 20, 2019 at 16:00
  • MSVC gives me four warnings, one for each occurrence. "warning C4477: 'scanf' : format string '%d' requires an argument of type 'int *', but variadic argument 1 has type 'double *'". As for the #include "stdio.h" it is not wrong: you put that when you want to use your own library instead of the standard library. Note that #include <stdbool.h> is the usual way: did the teacher's template use the two different ways to include a library header? Commented May 20, 2019 at 16:05
  • #ifdef _MSC_VER #define _CRT_SECURE_NO_WARNINGS #endif – Is pointless since Microsofts compiler won't ever be able to compile your code since it contains a VLA (Variable Length Array). Commented May 20, 2019 at 16:21

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

OP has other incorrect uses of double in the scanf calls: scanf("%d %d",&src.x,&src.y); and scanf("%d %d",&dest.x,&dest.y);.
This is true, but there is a problem with the array too as commented above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.