[英]Initialise 2 dimensional array of struct with all values zeroed
首先,我應該說我幾乎沒有C經驗,而當我說得很少時,我的意思是大約2 1/2個小時。 因此,請原諒並糾正任何不正確,愚蠢或其他個人缺陷。
這是當前的代碼:
typedef struct
{
float n;
int x;
int y;
int values[5];
} Cell;
typedef Cell Grid[10][10];
void update(Grid *source)
{
// This should be a 2D array of Cells.
// All the values in the Cell should be 0,
// including the contents of the values array.
Grid grid;
}
更新將被相當頻繁地調用,並且在某種程度上對性能至關重要,因此如果為了提高性能,我願意犧牲一些可讀性/簡單性/編碼時間。 不,這不是過早的優化。
謝謝你的幫助,
山姆
最簡單,最快的方法是memset
陣列:
memset(grid, 0 sizeof(Cell)*10*10);
實際上, grid
大小是在編譯時知道的,因此
memset(grid, 0, sizeof(Grid));
應該足夠了。
這將初始化您的數組。
Grid grid={0};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.