gcc 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?
Either
or
Or, if you prefer
but not what you have in your original variant.
Note, that in your specific case in all variants you can use either
&dev_sysordev_sysas the first argument. The effect will be the same. However,&dev_sysis more appropriate in the first variant, since if follows thememset(ptr-to-object, object-size)idiom. In the second and third variants it is more appropriate to usedev_sys(or&dev_sys[0]), since it follows thememset(ptr-to-first-element, number-of-elements * element-size)idiom.P.S. Of course, instead of using all that hackish
memsettrickery, in your particular case you should have just declared your array with an initializerNo
memsetnecessary.