在C中的深度复制数组... malloc? [英] Deep copying array in C... malloc?
本文介绍了在C中的深度复制数组... malloc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在C中复制数组的深层副本(原始板是副本):
I'm trying to make a deep copy of an array in C (originalBoard being the copy):
int gy, gx;
for (gy=0; gy<9; gy++)
{
for (gx=0; gx<9; gx++)
{
g.originalBoard[gy][gx]=g.board[gy][gx];
}
}
这似乎没有解决,我猜测这只是指向原始板阵列的指针.
This does not seem to be working out, and I'm guessing this is just making pointers to the original board array.
那么解决方案是尝试使用malloc吗?喜欢:
So would the solution be to try and use malloc? Like:
int* g.originalBoard[9][9]=malloc(sizeof(g.board[9][9]));
顺便说一句,这是一个9x9的二维数组.语法是什么(编译器在上一行中给出了错误...)?
btw this is a 9x9 two dimensional array. What would the syntax be (the compiler gives an error for the above line...)?
推荐答案
我认为您需要这个:
//assuming g.originalBoard is array of array of integers and same for g.board
int *originalBoard = malloc(sizeof(g.board));
memcpy(originalBoard, g.board, sizeof(g.board));
这篇关于在C中的深度复制数组... malloc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文