[英]Initialize 2D array in C using array of pointers
I have a static array that I want to use to initialize a dynamic array using pointers.我有一个 static 数组,我想用它来使用指针初始化动态数组。
Right now I have this:现在我有这个:
boardinit[BOARD_HEIGHT][BOARD_LENGTH] = {
{-4, -2, -3, -5, -6, -3, -2, -4},
{-1, -1, -1, -1, -1, -1, -1, -1},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 1},
{4, 2, 3, 5, 6, 3, 2, 4},
};
int main()
{
int board[BOARD_HEIGHT][BOARD_LENGTH];
initboard(board);
for (int j = 0; j < BOARD_HEIGHT; j++)
{
for (int i = 0; i < BOARD_LENGTH; i++)
printf("%d ", board[j][i]);
printf("\n");
}
return 0;
}
void initboard(int (*pboard)[BOARD_HEIGHT])
{
for(int i = 0;i<BOARD_HEIGHT;i++)
pboard[i] = boardinit + i;
}
I want board[BOARD_HEIGHT][BOARD_LENGTH]
to initialize as boardinit[BOARD_HEIGHT][BOARD_LENGTH]
by passing board to initboard()
using pointers, but can't seem to get it to go.我希望
board[BOARD_HEIGHT][BOARD_LENGTH]
BOARD_LENGTH] 通过使用指针将 board 传递给initboard()
来初始化为boardinit[BOARD_HEIGHT][BOARD_LENGTH]
,但似乎无法将其传递给 go。
To copy the contents boardinit
to the value of board
, just use this:要将内容
boardinit
复制到board
的值,只需使用以下命令:
memcpy(board, boardinit, sizeof(board));
If that does not work, try this:如果这不起作用,请尝试以下操作:
void initboard(int pboard[BOARD_HEIGHT][BOARD_LENGTH]) {
for (int i = 0; i < BOARD_HEIGHT; i++)
for (int j = 0; j < BOARD_LENGTH; j++)
pboard[i][j] = boardinit[i][j];
}
"I have a static array that i want to use to initialize a dynamic array using pointers." “我有一个 static 数组,我想用它来使用指针初始化一个动态数组。”
The problem with arrays of pointers is that each call to [m][c]alloc()
is not guaranteed (in fact is not likely) to provide a single contiguous block of memory. arrays 指针的问题在于,每次调用
[m][c]alloc()
都不能保证(实际上不太可能)提供 memory 的单个连续块。 ie, this is a common approach that provide many little areas of memory:即,这是提供 memory 的许多小区域的常用方法:
int **array = malloc(sizeof(width * sizeof(int *));
{
for(int i=0; i<width; i++)
{
array[i] = malloc(height*sizeof(int));
}
}
Thus, the initializer would need to be broken into several memory block locations.因此,初始化程序需要分成几个 memory 块位置。
A simpler and more efficient way to do this is with a single dynamic memory allocation call, guaranteeing a single contiguous block of memory, then a single memcpy()
is possible using the initializer into the pointer location returned from [m][c]alloc()
.一种更简单、更有效的方法是使用单个动态 memory 分配调用,保证 memory 的单个连续块,然后可以使用初始化器到从
[m][c]alloc()
返回的指针位置执行单个memcpy()
[m][c]alloc()
。 The only problem then is how to access the memory area with array notation.那么唯一的问题是如何使用数组表示法访问 memory 区域。 The example below illustrates how this can be done using
*(ptr + index)
notation...下面的示例说明了如何使用
*(ptr + index)
表示法来完成...
//assuming your other code above
int main(void)
{
int elements = BOARD_HEIGHT*BOARD_LENGTH;
int *pArray = calloc(elements, sizeof(int));
if(pArray)
{
memcpy(pArray, boardinit, sizeof boardinit);
for(int i=0; i<BOARD_HEIGHT;i++)
for(int j = 0;j<BOARD_LENGTH;j++)
printf("%d\n", *(pArray + (i + j)));
free(pArray);
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.