您正在写入不属于您的内存:
int board[2][50]; //make an array with 3 columns (wrong)
//(actually makes an array with only two 'columns')
...
for (i=0; i<num3+1; i++)
board[2][i] = 'O';
^
更改这一行:
int board[2][50]; //array with 2 columns (legal indices [0-1][0-49])
^
To:
int board[3][50]; //array with 3 columns (legal indices [0-2][0-49])
^
创建数组时,用于初始化的值:[3]
表示数组大小。
但是,当访问现有数组元素时,索引值是零基础.
对于创建的数组:int board[3][50];
合法索引为 board[0][0]...board[2][49]
EDIT解决错误的输出注释和初始化注释
添加额外的“\n”以格式化输出:
改变:
...
for (k=0; k<50;k++) {
printf("%d",board[j][k]);
}
}
...
To:
...
for (k=0; k<50;k++) {
printf("%d",board[j][k]);
}
printf("\n");//at the end of every row, print a new line
}
...
初始化板变量:
int board[3][50] = {0};//initialize all elements to zero
( 数组初始化讨论... https://stackoverflow.com/questions/40590302/initializing-a-multidimensional-array-0-versus-0 )