首页 > 解决方案 > 2D array not updating even after rewriting elements right after

问题描述

CONTEXT:

Hello, I'm trying to print a 7x6 Connect Four board where each section is |___| with three underscores. I am trying to create each center underscore an element of a 2D array so I can later update it for player moves. I am currently trying to test updating the center underscore, or array element.

CONFLICT:

A successful test would output a selected section as |_X_|. I tried testing updating an element inside PrintBoard() with arr[1][1] = 'X'; however it remained as a _ and the section also remained as |___|. I then retried this in main() but with board[1][1] = 'X'; to no avail. I get no errors or warnings either.

CODE:

#include <stdio.h>

void PrintBoard(char arr[6][7]);

int main()
{

    // Declaration of 7x6 2D board array: board[row][col]
    char board[6][7];

    // Sets all board array elements to '_'
    PrintBoard(board);

    /* board[1][1] =  'X'; // NOT WORKING, ELEMENTS REMAIN AS `_` */

    return 0;
}

void PrintBoard(char arr[6][7])
{
    int vertCnt = 0; // Counts vertical lines (8 per row, separates sections)
    int undCnt = 0; // Counts underscores (3 per section)
    int rowCnt = 0; // Counts rows (6 total)
    int colCnt = 0; // Count columns (7 total)

    // Print game title
    printf("      ~~ CONNECT FOUR ~~\n\n");

    for (int rowCnt = 0; rowCnt <= 6; rowCnt++)
        {
            // If current row is not the first, start it on a new line
            if (rowCnt > 0)
            {
                printf("\n");
            }

            // Creation of row: |___|___|___|___|___|___|___|
            for (int vertCnt = 0; vertCnt < 8; vertCnt++)
            {
                printf("|");

                // Only print `_` three times as long as there have been 7 total or less vertical lines printed
                for (int undCnt = 0; undCnt < 3 && vertCnt <= 6; undCnt++)
                {

                    // Print left and right sections as `_`
                    if(undCnt != 1)
                    {
                        printf("_");
                    }

                    // Assign middle section to board array and prints it as `_`
                    else if(undCnt == 1)
                    {
                        // If printing left underscore, increment column count
                        if(colCnt < 7){colCnt++;}

                        // Assign middle section to 2D board array
                        arr[rowCnt][colCnt] = '_';
                        printf("%c", arr[rowCnt][colCnt]);

                        // Test to rewrite random array element
                        arr[1][1] = 'X'; // NOT WORKING. ELEMENTS REMAIN AS `_`

                        // After last (7th) column reached, reset to 0
                        if(colCnt == 7){colCnt = 0;}
                    }

                }
            }
        }

    // Print column numbers
    printf("\n  1   2   3   4   5   6   7\n\n");

    /* HOW A CLEAN BOARD SHOULD LOOK:

         ~~ CONNECT FOUR ~~             <--- GAME TITLE

    |___|___|___|___|___|___|___|
    |___|___|___|___|___|___|___|
    |___|___|___|___|___|___|___|       <--- BOARD
    |___|___|___|___|___|___|___|
    |___|___|___|___|___|___|___|
    |___|___|___|___|___|___|___|
      1   2   3   4   5   6   7         <--- COLUMN NUMBERS

    */

}

标签: carraysmultidimensional-array

解决方案


char[7]范围 from0到的有效数组索引<= 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; }

有效地colCnt1to7而不是0to 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

推荐阅读