复制一个 3 dim 数组本身

标签 c arrays copy

我必须在 C 中创建一个数独游戏,对于“撤消”功能,我想复制前 3 个暗淡的。数组进入下一个。

问题是我的程序在 i=j=0 时中断,所以它甚至没有开始复制数组。

这是我的:

void copydim(int sudoku[z][9][9])
{
    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 9; j++)
        {
            sudoku[dim + 1][i][j] = sudoku[dim][i][j];
        }
    }
}

z 定义为 10。

ij 是数独的行和列。

这是调用:它是一个简单的切换指令,当用户按下 1 时应该复制数独

case 49:
    if (sudoku[dim][yvek][xvek] >= 0)
    {
        copydim(sudoku[z][9][9]); /*my debugger says that the sudoku array has the right values here, but in the next step when my programm switches into the copydim function there are no more values and an error occurs, although the pointer to the sudoku is the same as in this function :(*/
        sudoku[dim][yvek][xvek] = 1;
        editanzeige(sudoku, x, y);
    }
    break;

我的数组声明在我的主函数中。

最佳答案

评论中的这一行:

copydim(sudoku[z][9][9]);

正在传递数独数组的单个实例中的单个字段。

事实上,一个位于 sudoku[][9][9] 数组的单个实例范围之外的字段。这是未定义的行为,可能会导致段错误事件。

真正需要的是传递一个地址。

copydim( &sudoku );

建议:

#define MAX_ROWS (9)
#define MAX_COLS (9)
#define MAX_GAMEBOARDS (10)

struct gameboard
{
    int rows[ MAX_ROWS ];
    int cols[ MAX_COLS ];
};

// declare the array of gameboards with name `sudoku`
struct gameboard sudoku[ MAX_GAMEBOARDS];

int dim =0;

其中 copydim() 的原型(prototype)为:

void copydim( struct gameboard * );

调用子函数的地方:

int main( void )
{
    ...
    copydim(sudoku);
    ...
    return 0;
}

关于复制一个 3 dim 数组本身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34717860/

相关文章:

c - 如何将位域及其内部的位传递给函数

java - 从 0 到 99999 之间的数字中提取数字

java - 在 Java 中访问和引用 HashMap 时遇到问题

arrays - 快速将协议(protocol)数组转换为任何对象

c++ - 指向包含 vector<int> 问题的实例类的指针

enums - Enum导出trait Copy时的 "trait Clone is is not implemented"

Ruby Mechanize gem,从本地 html 副本恢复 Mechanize::Page 对象

填充数组时崩溃

c - 必须包含哪些 header 才能将 wsprintf 与 MinGW 一起使用

c - 无法比较多个字符