Static structure members
I would like to ask you: Are the members of a static structure initialized to zero? For example:
static struct _radioSettings
{
unsigned char radio_in;
unsigned char radio_out;
}radioSettings;
So this structure is housed in the radio-settings.c module. If radioSettings.radio_in and radioSettings.radio_out are not initialized to zero on compilation, how can I initialize them inside the radio-settings.c module?
source to share
Static, in C, has to do with the visibility of a structure, it means nothing other than that it is not visible from an external module radio-settings.c
.
Structures in C are not initialized with anything. The values for its fields are the memory values that the structure fell into. Thus, you cannot count on anything like this.
If you want to initialize a struct, then it's simple:
memset( &radioSettings, 0, sizeof( _radioSettings ) );
You only need to put this in a function init()
for radioSettings settings, inside a moduleradio-settings.c
Hope it helps.
source to share
All global variables are initialized with default values.
Section 6.7.8 Initializing the C99 Standard (n1256) says:
If an object with automatic storage duration is not explicitly initialized, its value is undefined. If an object that has static storage duration is not explicitly initialized, then:
- if it has a pointer type, it is initialized with a null pointer;
- if it is of arithmetic type, it is initialized (positive or unsigned) with zero;
- if it is an aggregate, each member is initialized (recursively) according to these rules;
- if it is a union, the first named element is initialized (recursively) according to these rules.
So, for your structure, each field is initialized with a default value of 0 .
source to share