C编程char ** for循环不会退出

时间:2017-10-05 20:04:00

标签: c pointers char maze

我是C语言编程的新手,我对以下问题感到困惑:

  • 用户首先输入其迷宫的列数和行数(按此顺序)
  • 他们将“迷宫”放入控制台(星号是墙,空格代表路径,S和F代表开始和结束),如下所示:

    ****S  ***
    *   ** ***
    ***    **F
    *** ***** 
    *** 
    

我已将维度存储到名为height和width的int变量中,这就是我的代码现在的样子:

    char **maze;
    bool **wasHere;
    int width, height;
    int startX, startY;
    int endX, endY;

    //Maze dimension one is the rows, maze dimension two is the 
    columns
    int main() {
       //Step one: initialize matrices
       scanf("%d%d\n", &width, &height);

       maze = malloc(height * sizeof(char*)); //dimension 1 = row#

       for (int i = 0; i < height; i++){
              maze[i] = malloc((width + 2) * sizeof(char));
        }
/* From now on, maze[x][y] will refer to the (x,y) element of the maze,
 i.e., x-th row and y-th column in the maze */

//Set the wasHere boolean array to all false values
//I am trying to look at each row first, then move from left to right within that row
//then do the same for the rows below, until the row index is [height - 1]

//Step two: store the maze by reading in each row at a time
for (int i = 0; i < height; i++){
    //fgets reads in each row
    fgets(maze[i], width + 2, stdin);
    printf("\nStored row %d\n", i);
}

我一直用35行60列的迷宫测试它,但for循环只运行到i = 15! 任何想法???

0 个答案:

没有答案