简体   繁体   English

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

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

I am new to the programming language C.我是编程语言 C 的新手。 I am trying to make a simple game where you can walk around in a 2D space and place or remove blocks.我正在尝试制作一个简单的游戏,您可以在其中四处走动并放置或删除块。 I am using the terminal for the graphics.我正在使用终端来绘制图形。 The problem is, I expect the program to print an X character followed by 24 blank lines, however, the program just straight up crashes.问题是,我希望程序打印一个X字符后跟 24 个空白行,但是,程序直接崩溃了。 I don't understand how or why my code could have crashed.我不明白我的代码如何或为什么会崩溃。

Here is the code:这是代码:

#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;
}

Can you explain the problem and show the correct way to do it.你能解释这个问题并展示正确的方法吗? Thanks in advance - justAnotherCoder在此先感谢 - justAnotherCoder

EDIT: The problem is fixed now.编辑:问题现在已解决。 Thank you for your help.谢谢您的帮助。

The program has undefined behavior at least because even in the first loop there is used an initialized variable x该程序至少具有未定义的行为,因为即使在第一个循环中也使用了初始化变量 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 */

And moreover the conditions x <= 24 and y <= 79 in the loops are also invalid.此外,循环中的条件x <= 24y <= 79也是无效的。

The loops do not have sense.循环没有意义。

The array map is declared like数组map声明如下

char map[79][24];

that is it has 79 rows and 24 columns.也就是说它有79行和24列。

So the loops should look like所以循环应该看起来像

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

The same indices and conditions should be use in the second pair of loops.在第二对循环中应该使用相同的索引和条件。

Instead of the loops that initialize the array you could use the standard string function memset like您可以使用标准字符串 function memset而不是初始化数组的循环

#include <string.h>

//,,,

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

Here is a demonstrative program (for simplicity I reduced the number of rows from 79 to 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;
}

The program output is程序 output 是

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

Edit: Take into account that after you changed your code nevertheless if to ignore the invalid conditions then you are outputting a figure that contains 80 columns and 25 rows instead of outputting a figure with 25 columns and 80 rows编辑:考虑到在您更改代码之后,如果忽略无效条件,那么您将输出一个包含 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 */

because the inner loop outputs sequentially in one line 80 characters.因为内部循环在一行中按顺序输出 80 个字符。

Check for loops condition you have written in your code.检查您在代码中编写的循环条件。 for achieving the expected result use below modified code为了达到预期的结果使用下面的修改代码

#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;
}

Edit: keep the variables scope with assigned blocks of memory.编辑:将变量 scope 与分配的 memory 块一起保留。

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

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