The initializer for an array contains the = symbol followed by a comma-separated list of constant expressions enclosed in braces ({ }). You do not need to initialize all elements in an array. Elements that are not initialized (in extern and static definitions only) receive the value 0 of the appropriate type. ...
The following show four different character array
initializations:
static char name1[] = { 'J', 'a', 'n' };
static char name2[] = { "Jan" };
static char name3[3] = "Jan";
static char name4[4] = "Jan";
These initializations create the following elements:
| Element | Value | Element | Value | Element | Value | Element | Value |
| name1[0] | J | name2[0] | J | name3[0] | J | name4[0] | J |
| name1[1] | a | name2[1] | a | name3[1] | a | name4[1] | a |
| name1[2] | n | name2[2] | n | name3[2] | n | name4[2] | n |
| name2[3] | \0 | name4[3] | \0 |
Note that the NULL character is lost for name1[] and name3
[3]. In C, a compiler warning is issued for name3[3 ]. In C++,
the compiler issues a severe error for name3[3 ].
Initializing a multidimensional array
Initialize a multidimensional array by:
static month_days[2][12] =
{
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
static int month_days[2][12] =
{
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};
You cannot have more initializers than the number of elements in
the array.