Reputation: 87
#include <stdio.h>
#include <string.h>
typedef struct STest
{
unsigned int uiRoll;
unsigned short usiVal;
} TTest;
int main()
{
TTest oTest = {0}; /* Initialize the Values with 0 */
printf( "The values are: %d, %d\n", oTest.uiRoll, oTest.usiVal );
return 0;
}
Observation:
gcc -Wextra -o a aa.cpp
warning: missing initializer for member STest::usiVal
How can I initialize the structure? so that there is no warning message.
Upvotes: 3
Views: 2315
Reputation: 14103
This notation is the most portable/correct way to zero initialize a structure:
#include <stdio.h>
#include <string.h>
typedef struct STest
{
unsigned int uiRoll;
unsigned short usiVal;
} TTest;
int main()
{
/* Static declaration causes structure member's to be zeroed at instantiation */
static const TTest zeroed_struct;
/* Structure assignment is then safely used */
TTest oTest = zeroed_struct;
printf( "The values are: %d, %d\n", oTest.uiRoll, oTest.usiVal );
return (0);
}
Declaring a static TTest
structure causes the compiler to automatically set all the structure fields to zero. You can then safely use the assignment operator to initialize another instance of your structure's members to zero.
It seems to me that using the universal zero initializer like you did is correct. Nevertheless, I found this interesting post on the GCC bugtracker which would explain why a warning is issued on your version of GCC, probably version 4.4: Bug 53119 - -Wmissing-braces wrongly warns about universal zero initializer {0}.
Upvotes: 2
Reputation: 206727
Why don't you initialize both members?
TTest oTest = {0, 0};
Or, possibly better:
TTest oTest = { .uiRoll = 0, .usiVal = 0};
(This assumes that you are indeed writing C code, which should be compiled with gcc
and saved with an extension that is .c
. If you save that code in a file with a .cpp
extension, gcc
will switch to C++ mode, and fail on that second version.)
Another option: upgrade your compiler to GCC >= 4.7.2 (possibly plain 4.7.0 would be enough). That warning is removed and you can use the short form you have in your code.
Upvotes: 1
Reputation:
Aside of the fact that GCC in this case is could actually be right, the general rule of thumb to disable warnings is to add no-
prefix to an option that controls emission of a warning message for a particular case. For example, if you read gcc's manual page, it has the following:
An aggregate has an initializer which does not initialize all members. This warning can be independently controlled by -Wmissing-field-initializers.
Therefore, in order to disable this option, you have to pass -Wno-missing-field-initializers
to GCC.
Interestingly enough, GCC 4.2.1 issues this warning while GCC 4.7.2 does not, even if that warning is enabled explicitly. After a bit more experimentation, it seems like if you specify a single 0
, no warning is given. However, if you specify two or more fields but not all of them, the warning is there.
Hope it helps. Good Luck!
Upvotes: 0
Reputation: 9385
or even:
TTest oTest;
memset(&oTest, 0, sizeof(oTest));
I don't get a warning. How did you try to use memset?
Upvotes: 0