struct SampleStruct {
int a;
int b;
float c;
double d;
short e;
};
For an array like this, I used to initialize it as below:
struct SampleStruct sStruct = {0};
I would like to know when I declare array of this structure, I thought this will be correct
struct SampleStruct sStructs[3] = {{0},{0},{0}};
But, below also got accepted by the compiler
struct SampleStruct sStructs[3] = {0};
I would like to know the best and safe way and detailed reason why so.
If using
-Walloption, my gcc gives me warnings about the third one:indicating that you should write
= {{0}}for initialization, which set the first field of the first struct to 0 and all the rest to 0 implicitly. The program gives correct result in this simple case, but I think you shouldn’t rely on this and need to initialize things properly.