繁体   English   中英

在 for 循环崩溃程序中打印二维数组

[英]Printing 2D array in a for loop crashes program

我是编程语言 C 的新手。 我正在尝试制作一个简单的游戏,您可以在其中四处走动并放置或删除块。 我正在使用终端来绘制图形。 问题是,我希望程序打印一个X字符后跟 24 个空白行,但是,程序直接崩溃了。 我不明白我的代码如何或为什么会崩溃。

这是代码:

#include <stdio.h>

int main() {
    char map[79][24]; /* create the map */
    int xPosition = 0, yPosition = 0; /* set the x and y position for the player */
    int x, y; /* create variables for the for loop */
    for (y = 0; y <= 24; y++) { 
        for (x = 0; x <= 79; x++) {
            map[x][y] = ' ';
        }
    } /* initialize the map */
    map[xPosition][yPosition] = 'X'; /* put the player in their position */
    for (y = 0; y <= 24; y++) {
        for (x = 0; x <= 79; x++) {
            printf("%c", map[x][y]);
        }
        printf("\n");
    } /* display the map on screen */
    return 0;
}

你能解释这个问题并展示正确的方法吗? 在此先感谢 - justAnotherCoder

编辑:问题现在已解决。 谢谢您的帮助。

该程序至少具有未定义的行为,因为即使在第一个循环中也使用了初始化变量 x

int x, y; /* create variables for the for loop */
for (y = 0; x <= 24; x++) { 
            ^^^^^^^
    for (x = 0; y <= 79; y++) {
        map[x][y] = ' ';
    }
} /* initialize the map */

此外,循环中的条件x <= 24y <= 79也是无效的。

循环没有意义。

数组map声明如下

char map[79][24];

也就是说它有79行和24列。

所以循环应该看起来像

for (x = 0; x < 79; x++) { 
    for (y = 0; y < 24; y++) {
        map[x][y] = ' ';
    }
} /* initialize the map */

在第二对循环中应该使用相同的索引和条件。

您可以使用标准字符串 function memset而不是初始化数组的循环

#include <string.h>

//,,,

memset( map, ' ', 79 * 24 );

这是一个演示程序(为简单起见,我将行数从 79 减少到 24)

#include <stdio.h>
#include <string.h>

int main(void) 
{
    enum { M = 24, N = 24 };

    char map[M][N];

    memset( map, ' ', M * N );

    map[0][0] = 'X';


    for ( size_t i = 0; i < N + 2; i++ )
    {
        putchar( '-' );
    }
    putchar( '\n' );

    for ( size_t i = 0; i < M; i++ )
    {
        printf( "|%.*s|\n", N, map[i] );
    }

    for ( size_t i = 0; i < N + 2; i++ )
    {
        putchar( '-' );
    }
    putchar( '\n' );

    return 0;
}

程序 output 是

--------------------------
|X                       |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
--------------------------

编辑:考虑到在您更改代码之后,如果忽略无效条件,那么您将输出一个包含 80 列和 25 行的图形,而不是输出一个包含 25 列和 80 行的图形

for (y = 0; y <= 24; y++) {
    for (x = 0; x <= 79; x++) {
        printf("%c", map[x][y]);
    }
    printf("\n");
} /* display the map on screen */

因为内部循环在一行中按顺序输出 80 个字符。

检查您在代码中编写的循环条件。 为了达到预期的结果使用下面的修改代码

#include <stdio.h>

int main() {
    char map[79][24]; /* create the map */
    int xPosition = 0, yPosition = 0; /* set the x and y position for the player */
    int x, y; /* create variables for the for loop */
    for (x = 0; x < 24; x++) {
        for (y = 0; y < 79; y++) {
            map[x][y] = ' ';
        }
    } /* initialize the map */
    map[xPosition][yPosition] = 'X'; /* put the player in their position */
    for (x = 0; x < 24; x++) {
        for (y = 0; y < 79; y++) {
            printf("%c", map[x][y]);
        }
        printf("\n");
    } /* display the map on screen */
    return 0;
}

编辑:将变量 scope 与分配的 memory 块一起保留。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM