Допустимые индексы массива для диапазона char[7]
от 0
до <= 6
.
Доступ к массиву char arr[6][7]
за пределами:
for (int rowCnt = 0; rowCnt <= 6; rowCnt++)
считается до 6
, где он должен считаться только до 5
, а конструкция
if (colCnt < 7) { colCnt++; }
arr[rowCnt][colCnt] = '_'; // eventually arr[6][something] gets
printf("%c", arr[rowCnt][colCnt]); // written to --> corupted stack.
if (colCnt == 7) { colCnt = 0; }
фактически составляет colCnt
с 1
до 7
вместо 0
до 6
.
И вы слишком усложняете вещи ...
#include <stddef.h>
#include <stdio.h>
enum { board_width = 7, board_height = 6 };
void board_print(char arr[board_height][board_width]);
int main(void)
{
char board[board_height][board_width] = { 0 };
board_print(board);
board[1][1] = 'X';
board_print(board);
}
void board_print(char arr[board_height][board_width])
{
puts(" ~~ CONNECT FOUR ~~\n");
for (size_t row = 0; row < board_height; ++row) {
for (size_t col = 0; col < board_width; ++col) {
printf("|_%c_", arr[row][col] ? arr[row][col] : '_');
}
puts("|");
}
puts(" 1 2 3 4 5 6 7\n");
}
Вывод:
~~ CONNECT FOUR ~~
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
1 2 3 4 5 6 7
~~ CONNECT FOUR ~~
|___|___|___|___|___|___|___|
|___|_X_|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
1 2 3 4 5 6 7