Home >Backend Development >C++ >How are Automatic Structures and Arrays Partially Initialized in C and C ?
Partial Initialization of Automatic Structures and Arrays in C and C
The GNU C Reference Manual states that if a structure variable is not initialized, the values of its members are indeterminate. However, the standards do not define partial initialization of structures or arrays.
Partial Initialization Terminology
"Partial initialization" refers to providing some, but not all, initializers for an aggregate (array or structure).
Initialization Rules for Automatic Structures and Arrays
The C and C standards specify the following rules for initialization of automatic structures and arrays:
Partial Initialization Behavior
Even though partial initialization is not explicitly defined in the standards, all mainstream compilers follow the following behavior:
Example
The following C code partially initializes a structure:
struct S { int a; char* b; int c; }; S s = {1, "asdf"};
In this example, s.a is initialized to 1, s.b is initialized to "asdf", and s.c is zero-initialized because it is not explicitly initialized.
The above is the detailed content of How are Automatic Structures and Arrays Partially Initialized in C and C ?. For more information, please follow other related articles on the PHP Chinese website!