在 for 循环中打印二维数组会使程序崩溃
Printing 2D array in a for loop crashes program
我是编程语言 C 的新手。我正在尝试制作一个简单的游戏,您可以在其中在 2D space 中四处走动并放置或移除块。我正在使用图形终端。问题是,我希望程序打印一个 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
编辑:问题现已解决。感谢您的帮助。
检查您在代码中编写的循环条件。为了达到预期的结果,请使用下面修改后的代码
#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;
}
编辑: 将变量范围保留在分配的内存块中。
该程序至少有未定义的行为,因为即使在第一个循环中也使用了一个初始化变量 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 <= 24
和y <= 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 */
第二对循环中应使用相同的索引和条件。
您可以使用标准字符串函数 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;
}
程序输出为
--------------------------
|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个字符顺序输出
我是编程语言 C 的新手。我正在尝试制作一个简单的游戏,您可以在其中在 2D space 中四处走动并放置或移除块。我正在使用图形终端。问题是,我希望程序打印一个 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
编辑:问题现已解决。感谢您的帮助。
检查您在代码中编写的循环条件。为了达到预期的结果,请使用下面修改后的代码
#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;
}
编辑: 将变量范围保留在分配的内存块中。
该程序至少有未定义的行为,因为即使在第一个循环中也使用了一个初始化变量 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 <= 24
和y <= 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 */
第二对循环中应使用相同的索引和条件。
您可以使用标准字符串函数 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;
}
程序输出为
--------------------------
|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个字符顺序输出