首页 > 解决方案 > C内存棋盘游戏错误:读取访问冲突

问题描述

我正在对使用 C 语言创建的游戏进行一些改进。该项目是一个记忆棋盘游戏,其中有 2 个表格,一个数字表格和一个字母表格,玩家必须选择 2 个具有匹配字母的数字。

数字表是一个字符数组,每次正确猜测后,表中的数字都会被替换为字母表中的相应字母。

该错误发生在程序应该打印数字数组的函数的一部分中。(我已将形式数组参数大小设置为 8x8 以适应所有级别的游戏)。

玩家在ConditionCheck函数中选择数字,程序确保该数字有效。

void NumInput(char numbers[8][8], int rows, int columns)
{
    int i, j, n1, n2;
    printf("Choose 2 numbers from the table below: \n\n");       /// print the numbers array
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < columns; j++)
            if (isalpha(numbers[i][j]) != 0)        //  if the array literal is a letter
                printf("%2c  ", numbers[i][j]);
            else
                printf("%2d  ", numbers[i][j]);
        printf("\n\n");
    }

    n1 = ConditionCheck(1, rows * columns);
    n2 = ConditionCheck(2, rows * columns);
    N1 = &n1;
    N2 = &n2;
    return;
}

这就是我得到的地方:读取访问冲突。第 8 行中的数字是 0x11100DE(如果选择)

我做错了什么?

编辑:我已经在主游戏函数中初始化了数组。

char numbers[9][9];
k = 1;
for (i = 0; i < rows; i++)                /// initializing 'numbers' array
        for (j = 0; j < columns; j++)
        {
            numbers[i][j] = k;
            k++;
        }

我稍后在同一个主游戏函数中调用了函数NumInput几行:

while (countdown >= 0 && !win)
    {
        time1 = clock();
        NumInput(numbers[rows][columns], rows, columns);
        num1 = *N1;
        num2 = *N2;

注意: N1N2是整数指针,用于访问玩家在NumInput函数中选择的数字

标签: cvisual-studio-2017c-strings

解决方案


推荐阅读