为什么在请求用户的另一个输入后 char 数组为空?
Why is the char array empty after requesting another input from user?
我正在用 C 语言开发 TicTacToe,但在显示玩家姓名时遇到问题。
每次玩家必须采取行动时,我都会显示 "It's your turn, %s!"。
但是从第二次迭代开始,玩家名是空的。
经过一些测试,我发现当我注释第 56 行时,问题并没有发生。
控制台输出:
Starting...
Type your username (20 characters): Paulo
Type your username (20 characters): Edson
A B C
0 ___|___|___
1 ___|___|___
2 ___|___|___
It's your turn, Paulo!
Choose a position (Ex.: B2, A0, etc): A2
Picked: A2
A B C
0 ___|___|___
1 ___|___|___
2 ___|___|___
It's your turn, !
Choose a position (Ex.: B2, A0, etc): A3
Picked: A3
A B C
0 ___|___|___
1 ___|___|___
2 ___|___|___
It's your turn, !
Choose a position (Ex.: B2, A0, etc): ^C
这是我的代码:
#include <stdio.h>
#include <string.h>
void pickposition(char * position) {
printf("\nChoose a position (Ex.: B2, A0, etc): ");
scanf(" %2s", position);
}
void printtable(char table[3][3]) {
printf("\n\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
char c = table[i][j];
if (c != 'O' && c != 'X') {
table[i][j] = '_';
}
}
}
printf(" A B C \n");
printf("0 _%c_|_%c_|_%c_\n", table[0][0], table[0][1], table[0][2]);
printf("1 _%c_|_%c_|_%c_\n", table[1][0], table[1][1], table[1][2]);
printf("2 _%c_|_%c_|_%c_\n", table[2][0], table[2][1], table[2][2]);
printf("\n\n");
}
void pickusername(char *name) {
printf("\nType your username (20 characters): ");
scanf("%20s", name);
}
int main() {
printf("\nStarting...\n");
char table[3][3];
char player1[20];
char player2[20];
pickusername(player1);
pickusername(player2);
int next = 1;
int counter = 0;
while (1) {
char nextname[20];
printtable(table);
if (next) {
strcpy(nextname, player1);
} else {
strcpy(nextname, player2);
}
printf("It's your turn, %s!\n", nextname);
char positionpicked[2];
pickposition(positionpicked);
printf("\nPicked: %s\n", positionpicked);
counter--;
if (counter == -20) {
break;
}
}
}
PS.: 我知道next
总是1,那只是草稿。
当我 运行 你的程序时,它对我来说运行良好。但是 positionpicked
数组的大小需要一个小的修正。您要求用户输入两个字符 (A0/B1),但 positionpicked
只有 2 个元素,这意味着没有分配内存来存储 '[=14=]
'。所以 positionpicked
的大小应该增加到 3。
这也可能是问题的原因(我不确定但很可能是)因为这个 NULL 字符有可能会覆盖 player1[0]。
在第一次迭代期间:-
player1 -> Pablo0
player2 -> Edson0
positionpicked -> A20 /* Since positionpicked can only store 2 bytes the 0('[=10=]')
is stored in the adjacent memory location */
在第二次迭代期间:-
player1 -> 0ablo0 /* Which will print a null string */
我正在用 C 语言开发 TicTacToe,但在显示玩家姓名时遇到问题。
每次玩家必须采取行动时,我都会显示 "It's your turn, %s!"。
但是从第二次迭代开始,玩家名是空的。
经过一些测试,我发现当我注释第 56 行时,问题并没有发生。
控制台输出:
Starting...
Type your username (20 characters): Paulo
Type your username (20 characters): Edson
A B C
0 ___|___|___
1 ___|___|___
2 ___|___|___
It's your turn, Paulo!
Choose a position (Ex.: B2, A0, etc): A2
Picked: A2
A B C
0 ___|___|___
1 ___|___|___
2 ___|___|___
It's your turn, !
Choose a position (Ex.: B2, A0, etc): A3
Picked: A3
A B C
0 ___|___|___
1 ___|___|___
2 ___|___|___
It's your turn, !
Choose a position (Ex.: B2, A0, etc): ^C
这是我的代码:
#include <stdio.h>
#include <string.h>
void pickposition(char * position) {
printf("\nChoose a position (Ex.: B2, A0, etc): ");
scanf(" %2s", position);
}
void printtable(char table[3][3]) {
printf("\n\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
char c = table[i][j];
if (c != 'O' && c != 'X') {
table[i][j] = '_';
}
}
}
printf(" A B C \n");
printf("0 _%c_|_%c_|_%c_\n", table[0][0], table[0][1], table[0][2]);
printf("1 _%c_|_%c_|_%c_\n", table[1][0], table[1][1], table[1][2]);
printf("2 _%c_|_%c_|_%c_\n", table[2][0], table[2][1], table[2][2]);
printf("\n\n");
}
void pickusername(char *name) {
printf("\nType your username (20 characters): ");
scanf("%20s", name);
}
int main() {
printf("\nStarting...\n");
char table[3][3];
char player1[20];
char player2[20];
pickusername(player1);
pickusername(player2);
int next = 1;
int counter = 0;
while (1) {
char nextname[20];
printtable(table);
if (next) {
strcpy(nextname, player1);
} else {
strcpy(nextname, player2);
}
printf("It's your turn, %s!\n", nextname);
char positionpicked[2];
pickposition(positionpicked);
printf("\nPicked: %s\n", positionpicked);
counter--;
if (counter == -20) {
break;
}
}
}
PS.: 我知道next
总是1,那只是草稿。
当我 运行 你的程序时,它对我来说运行良好。但是 positionpicked
数组的大小需要一个小的修正。您要求用户输入两个字符 (A0/B1),但 positionpicked
只有 2 个元素,这意味着没有分配内存来存储 '[=14=]
'。所以 positionpicked
的大小应该增加到 3。
这也可能是问题的原因(我不确定但很可能是)因为这个 NULL 字符有可能会覆盖 player1[0]。
在第一次迭代期间:-
player1 -> Pablo0
player2 -> Edson0
positionpicked -> A20 /* Since positionpicked can only store 2 bytes the 0('[=10=]')
is stored in the adjacent memory location */
在第二次迭代期间:-
player1 -> 0ablo0 /* Which will print a null string */