字符矩阵溢出
Char matrix overflow
代码很简单,我正在尝试做一个战舰游戏,但还处于早期阶段
#include <stdio.h>
void main()
{
int i = 0; /* Loop counter */
int player = 0; /* Player number - 1 or 2 */
int go = 0; /* Square selection number for turn */
int row = 0; /* Row index for a square */
int column = 0; /* Column index for a square */
int line = 0; /* Row or column index in checking loop */
int winner = 0; /* The winning player */
char board[8][8] = { /* The board */
{'1','2','3','4','5','6','7','8'}, /* Initial values are reference numbers */
{'9','10','11','12','13','14','15','16'}, /* used to select a vacant square for */
{'17','18','19','20','21','22','23','24'}, /* a turn. */
{'25','26','27','28','29','30','31','32'},
{'33','34','35','36','37','38','39','40'},
{'41','42','43','44','45','46','47','48'},
{'49','50','51','52','53','54','55','56'},
{'57','58','59','60','61','62','63','64'}
};
/* The main game loop. The game continues for up to 64 turns */
/* As long as there is no winner */
for( i = 0; i<64 && winner==0; i++)
{
/* Display the board */
printf("\n\n");
printf(" %2c | %2c | %2c | %2c | %2c | %2c | %2c | %2c\n", board[0][0], board[0][1], board[0][2], board[0][3], board[0][4], board[0][5], board[0][6], board[0][7]);
printf(" %2c | %2c | %2c | %2c | %2c | %2c | %2c | %2c\n", board[1][0], board[1][1], board[1][2], board[1][3], board[1][4], board[1][5], board[1][6], board[1][7]);
printf(" %2c | %2c | %2c | %2c | %2c | %2c | %2c | %2c\n", board[2][0], board[2][1], board[2][2], board[2][3], board[2][4], board[2][5], board[2][6], board[2][7]);
printf(" %2c | %2c | %2c | %2c | %2c | %2c | %2c | %2c\n", board[3][0], board[3][1], board[3][2], board[3][3], board[3][4], board[3][5], board[3][6], board[3][7]);
printf(" %2c | %2c | %2c | %2c | %2c | %2c | %2c | %2c\n", board[4][0], board[4][1], board[4][2], board[4][3], board[4][4], board[4][5], board[4][6], board[4][7]);
printf(" %2c | %2c | %2c | %2c | %2c | %2c | %2c | %2c\n", board[5][0], board[5][1], board[5][2], board[5][3], board[5][4], board[5][5], board[5][6], board[5][7]);
printf(" %2c | %2c | %2c | %2c | %2c | %2c | %2c | %2c\n", board[6][0], board[6][1], board[6][2], board[6][3], board[6][4], board[6][5], board[6][6], board[6][7]);
printf(" %2c | %2c | %2c | %2c | %2c | %2c | %2c | %2c\n", board[7][0], board[7][1], board[7][2], board[7][3], board[7][4], board[7][5], board[7][6], board[7][7]);
我正在尝试获得类似于
的输出
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
9 | 10 | 11 | 12 | 13 | 14 | 15 | 16
17 | 18 | 19 | 20 | 21 | 22 | 23 | 24
一直到 64,但我得到的是
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
9 | 0 | 1 | 2 | 3 | 4 | 5 | 6
7 | 8 | 9 | 0 | 1 | 2 | 3 | 4
等等...
只显示数字右边的数字
至于我得到的错误消息是关于 char board[8][8]
之后的内容
它适用于所有 8 行
Multiple markers at this line
-overflow in implicit constant conversion [-Woverflow]
-multi-character character constant [-Wmultichar]
我是 c/anci-c 的新手,所以任何信息都会有所帮助
这些不是有效字符 {'17','18','19','20','21','22','23','24'},
,因为它们由 2 个字符组成。如果你想存储超过 1 个字符的文本,你需要一个字符串,或者如果你想要数字,你应该删除单引号。无论哪种方式,您的 printf
语句也需要更改,因为您想要 %2s
或 %2d
您选择了 board
类型 char
,这意味着它包含的所有内容都应恰好为 1 个字符长。但是,您分配“10”、“11”等,它们包含 2 个字符。因此,编译器尝试将 2 个字符转换为 1 个字符的 char。但是由于 2 > 1,它会溢出,这会导致只分配一个字符(显然是最后一个字符)。此外,由于它基本上是对系统的一部分的猜测,它还会为您提供第二条错误消息。
您想将 board
类型从 char
更改为 string
或 int
(当然要删除数字周围的单引号)。您还希望 printf()
功能更改为正确的格式。
代码很简单,我正在尝试做一个战舰游戏,但还处于早期阶段
#include <stdio.h>
void main()
{
int i = 0; /* Loop counter */
int player = 0; /* Player number - 1 or 2 */
int go = 0; /* Square selection number for turn */
int row = 0; /* Row index for a square */
int column = 0; /* Column index for a square */
int line = 0; /* Row or column index in checking loop */
int winner = 0; /* The winning player */
char board[8][8] = { /* The board */
{'1','2','3','4','5','6','7','8'}, /* Initial values are reference numbers */
{'9','10','11','12','13','14','15','16'}, /* used to select a vacant square for */
{'17','18','19','20','21','22','23','24'}, /* a turn. */
{'25','26','27','28','29','30','31','32'},
{'33','34','35','36','37','38','39','40'},
{'41','42','43','44','45','46','47','48'},
{'49','50','51','52','53','54','55','56'},
{'57','58','59','60','61','62','63','64'}
};
/* The main game loop. The game continues for up to 64 turns */
/* As long as there is no winner */
for( i = 0; i<64 && winner==0; i++)
{
/* Display the board */
printf("\n\n");
printf(" %2c | %2c | %2c | %2c | %2c | %2c | %2c | %2c\n", board[0][0], board[0][1], board[0][2], board[0][3], board[0][4], board[0][5], board[0][6], board[0][7]);
printf(" %2c | %2c | %2c | %2c | %2c | %2c | %2c | %2c\n", board[1][0], board[1][1], board[1][2], board[1][3], board[1][4], board[1][5], board[1][6], board[1][7]);
printf(" %2c | %2c | %2c | %2c | %2c | %2c | %2c | %2c\n", board[2][0], board[2][1], board[2][2], board[2][3], board[2][4], board[2][5], board[2][6], board[2][7]);
printf(" %2c | %2c | %2c | %2c | %2c | %2c | %2c | %2c\n", board[3][0], board[3][1], board[3][2], board[3][3], board[3][4], board[3][5], board[3][6], board[3][7]);
printf(" %2c | %2c | %2c | %2c | %2c | %2c | %2c | %2c\n", board[4][0], board[4][1], board[4][2], board[4][3], board[4][4], board[4][5], board[4][6], board[4][7]);
printf(" %2c | %2c | %2c | %2c | %2c | %2c | %2c | %2c\n", board[5][0], board[5][1], board[5][2], board[5][3], board[5][4], board[5][5], board[5][6], board[5][7]);
printf(" %2c | %2c | %2c | %2c | %2c | %2c | %2c | %2c\n", board[6][0], board[6][1], board[6][2], board[6][3], board[6][4], board[6][5], board[6][6], board[6][7]);
printf(" %2c | %2c | %2c | %2c | %2c | %2c | %2c | %2c\n", board[7][0], board[7][1], board[7][2], board[7][3], board[7][4], board[7][5], board[7][6], board[7][7]);
我正在尝试获得类似于
的输出1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
9 | 10 | 11 | 12 | 13 | 14 | 15 | 16
17 | 18 | 19 | 20 | 21 | 22 | 23 | 24
一直到 64,但我得到的是
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
9 | 0 | 1 | 2 | 3 | 4 | 5 | 6
7 | 8 | 9 | 0 | 1 | 2 | 3 | 4
等等... 只显示数字右边的数字
至于我得到的错误消息是关于 char board[8][8]
之后的内容
它适用于所有 8 行
Multiple markers at this line
-overflow in implicit constant conversion [-Woverflow] -multi-character character constant [-Wmultichar]
我是 c/anci-c 的新手,所以任何信息都会有所帮助
这些不是有效字符 {'17','18','19','20','21','22','23','24'},
,因为它们由 2 个字符组成。如果你想存储超过 1 个字符的文本,你需要一个字符串,或者如果你想要数字,你应该删除单引号。无论哪种方式,您的 printf
语句也需要更改,因为您想要 %2s
或 %2d
您选择了 board
类型 char
,这意味着它包含的所有内容都应恰好为 1 个字符长。但是,您分配“10”、“11”等,它们包含 2 个字符。因此,编译器尝试将 2 个字符转换为 1 个字符的 char。但是由于 2 > 1,它会溢出,这会导致只分配一个字符(显然是最后一个字符)。此外,由于它基本上是对系统的一部分的猜测,它还会为您提供第二条错误消息。
您想将 board
类型从 char
更改为 string
或 int
(当然要删除数字周围的单引号)。您还希望 printf()
功能更改为正确的格式。