I'm creating some sort of frontend for a program. To launch the program I'm using the call CreateProcess()
, which among other things receives a pointer to a STARTUPINFO
structure. To initialize the structure I used to do:
STARTUPINFO startupInfo = {0}; // Or even '\0'.
startupInfo.cb = sizeof(startupInfo);
When compiling the program with GCC enabling these sets of warnings -Wall -Wextra
it gives me a warning saying that there's a missing initializer pointing to the first line.
warning: missing initializer
warning: (near initialization for 'startupInfo.lpReserved')
So I ended up doing:
STARTUPINFO startupInfo;
memset(&startupInfo, 0, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
And this way the compiler doesn't give any warning. The question is, what is the difference between these ways of initializing a structure? Using the first method, isn't the structure initialized? Which one would you recommend?
struct struct_with_four_fields x = {1, 2, 3};
where only 3 out of 4 members are initialized. – pmg Oct 8 '09 at 17:02{ 0 }
is a common and well-defined idiom for initializing all members to zero (defined recursively for each sub-member) -- which is why later versions of gcc have been modified not to warn about that particular case. – Keith Thompson Sep 9 '13 at 17:24obj = {0};
in the message you linked to is not valid C, and gcc 4.8.2 rejects it as a syntax error. If you're compiling as C++, remember that it's a different language, and gcc uses a different front end; fixes in gcc's C compiler may or may not apply to g++. – Keith Thompson Sep 12 '14 at 20:41