首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >即使在重写元素之后也不更新2D数组

即使在重写元素之后也不更新2D数组
EN

Stack Overflow用户
提问于 2018-10-07 14:19:25
回答 1查看 269关注 0票数 2

上下文:

您好,我正在尝试打印一张7x6连接四个板,其中每个部分都是带有三个下划线的|___|。我正在尝试创建每个中心,下划线一个二维数组的元素,这样我就可以在以后更新它以供玩家移动。我目前正在尝试更新中心下划线或数组元素。

冲突:

成功的测试会将选定的部分输出为|_X_|。我尝试用arr[1][1] = 'X';更新PrintBoard()中的一个元素,但是它仍然是一个_,该节也仍然是|___|。然后我在main()中重试了这一点,但是board[1][1] = 'X';没有用。我也没有收到任何错误或警告。

代码:

代码语言:javascript
复制
#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

    */

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-07 14:41:02

char[7]的有效数组索引范围从0<= 6

您越界访问数组char arr[6][7]

代码语言:javascript
复制
for (int rowCnt = 0; rowCnt <= 6; rowCnt++)

计数到6,其中它只应计数到5和构造

代码语言:javascript
复制
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; }

有效地将colCnt1连接到7,而不是从0连接到6

你把事情完全复杂化了.

代码语言:javascript
复制
#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");
}

输出:

代码语言:javascript
复制
      ~~ CONNECT FOUR ~~

|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
  1   2   3   4   5   6   7

      ~~ CONNECT FOUR ~~

|___|___|___|___|___|___|___|
|___|_X_|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
|___|___|___|___|___|___|___|
  1   2   3   4   5   6   7
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52685970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档