c - 我收到以下错误:“数组下标不是整数”。但该数组是 int 类型的。我做错了什么?
问题描述
偏差函数向我抛出以下错误:“数组下标不是整数”。如果您能帮助我找到错误原因,我将不胜感激。
#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;
}
“if”语句(在 // 后面)应该可以正常工作并在两个值都为 0 时输入。请注意,我将 2D 矩阵“arr”定义为 int 类型。为什么我会收到此错误?
我试图解决的问题是“二元迷宫中的最短路径”问题,但我一开始就卡住了。
解决方案
仔细阅读错误:它声明Array subscript is not an integer
. 有问题的错误是因为用于访问数组元素的值。
src.x
, src.y
,dest.x
和dest.y
是 类型double
。您需要重新定义point
为:
struct point
{
int x;
int y;
};
或者,如果您必须使用double
,则可以转换为int
.