[英]Array of 2D Array's dynamic allocated
我需要一個 Array:= arr of 2D Arrays:= multi_arr 而 2D Arrays 有一個固定大小,例如 9x9。 arr 的大小可以變化,因此在不需要緩沖區時需要重新分配。 如何為我的目的實例化指針? 我試過這樣的事情
int (*grid_ptr)[N][N] = malloc(sizeof(int) * buffer); // buffer is the size of multi_arrs
如何在此處添加二維數組? 我需要某種清單,這樣我才能做
grid_ptr[0] = grid;
這不起作用。
我還想將 multi_arrs 作為 function 中的參數傳遞,並通過引用將元素添加到 function 中的列表中。 我想我無法讀取數組/列表中填充元素的大小,所以我需要用一個變量來計算任何插入到 arr 的次數。
我想要的是偽代碼中的類似內容
int* getGrids(int* sizeOfGrid)
{
InitializeArray(int *grid);
while(condition){
doSomethingWithGrid(grid[0],&grid,&gridCount); // I pass the first Grid,
// get the whole Grid and Length by reference
// I want to add elements to arr and read values
// of multi_arr 2D array to the
// grid in the function
}
free(grid);
}
Arrays 沒有賦值運算符。
例如,您可以使用標准 C 字符串 function memcpy
memcpy( grid_ptr[0], grid, sizeof( grid ) );
grid 必須是數組才能正確使用 sizeof( grid )。 否則你需要使用一些變量來存儲指針網格指向的數組的大小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.