繁体   English   中英

初始化结构的二维数组,所有值均清零

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM