C语言 使用 memset 初始化结构体数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3389464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
initializing a structure array using memset
提问by ant2009
gcc 4.4.4 c89
海湾合作委员会 4.4.4 c89
I have the following structure.
我有以下结构。
struct device_sys
{
char device[STRING_SIZE];
int id;
char category;
};
int main(void)
{
struct device_sys dev_sys[NUM_DEVICES];
memset(dev_sys, 0, (size_t)NUM_DEVICES * sizeof(dev_sys));
return 0;
}
I get a stack dump when I call memset. Is this not the correct way to initialize an structure array?
当我调用 memset 时,我得到了一个堆栈转储。这不是初始化结构数组的正确方法吗?
回答by AnT
Either
任何一个
memset(&dev_sys, 0, sizeof dev_sys);
or
或者
memset(dev_sys, 0, NUM_DEVICES * sizeof(struct device_sys));
Or, if you prefer
或者,如果你喜欢
memset(dev_sys, 0, NUM_DEVICES * sizeof *dev_sys);
but not what you have in your original variant.
但不是原始变体中的内容。
Note, that in your specific case in all variants you can use either &dev_sysor dev_sysas the first argument. The effect will be the same. However, &dev_sysis more appropriate in the first variant, since if follows the memset(ptr-to-object, object-size)idiom. In the second and third variants it is more appropriate to use dev_sys(or &dev_sys[0]), since it follows the memset(ptr-to-first-element, number-of-elements * element-size)idiom.
请注意,在所有变体的特定情况下,您可以使用&dev_sys或dev_sys作为第一个参数。效果是一样的。但是,&dev_sys在第一个变体中更合适,因为 if 遵循memset(ptr-to-object, object-size)习语。在第二个和第三个变体中,使用dev_sys(or &dev_sys[0])更合适,因为它遵循memset(ptr-to-first-element, number-of-elements * element-size)习语。
P.S.Of course, instead of using all that hackish memsettrickery, in your particular case you should have just declared your array with an initializer
PS当然,不要使用所有那些骇人听闻的memset技巧,在您的特定情况下,您应该使用初始化程序声明您的数组
struct device_sys dev_sys[NUM_DEVICES] = { 0 };
No memsetnecessary.
没有memset必要。
回答by Hans Passant
There's a typo in your code. Fix:
你的代码有错别字。使固定:
memset(dev_sys, 0, (size_t)NUM_DEVICES * sizeof(struct device_sys));
Picking good names avoid half the bugs. I'd recommend "devices".
选择好名字可以避免一半的错误。我会推荐“设备”。
回答by Lucas
You have to pass the sizeofoperator the type and not the variable.
您必须向sizeof运算符传递类型而不是变量。
memset(dev_sys, 0, (size_t)NUM_DEVICES * sizeof(struct device_sys));
I prefer to use typedeffor the struct.
我更喜欢typedef用于结构。
typedef struct tag_device_sys
{
char device[STRING_SIZE];
int id;
char category;
} device_sys;
The you can use memsetas follows:
您可以memset按如下方式使用:
memset(dev_sys, 0, (size_t)NUM_DEVICES * sizeof(device_sys));
回答by Thom Smith
For an array, sizeofgets you the entire size of the array, not the size of an individual element. The sizeofoperator is one of the few places where an array is not treated as a pointer to its first element.
对于数组,sizeof获取数组的整个大小,而不是单个元素的大小。的sizeof操作者是,其中的阵列不是作为指针到它的第一元件处理的几个地方中的一个。

