iinniittiiaalliizzaattiioonn -- Definition

The  term _i_n_i_t_i_a_l_i_z_a_t_i_o_n  refers to  setting  a variable  to its  first, or
initial, value.

_R_u_l_e_s _o_f _I_n_i_t_i_a_l_i_z_a_t_i_o_n
Initializers follow the same rules for type and conversion as do assignment
statements.

If a static object with a  scalar type is not explicitly initialized, it is
initialized  to zero  by default.   Likewise,  if a  static pointer  is not
explicitly initialized, it is initialized to NULL by default.  If an object
with automatic storage duration is not explicitly initialized, its contents
are indeterminate.

Initializers  on  static  objects  must  be constant  expressions;  greater
flexibility  is allowed  for  initializers of  automatic variables.   These
latter  initializers  can  be  arbitrary  expressions,  not  just  constant
expressions.  For example,

    double dsin = sin(30.0);

is a valid initializer, where ddssiinn is declared inside a function.

To initialize  an object, use  the assignment operator  `='.  The following
sections describe how to initialize different classes of objects.

_S_c_a_l_a_r_s
To initialize  a scalar object, assign  it the value of  a expression.  The
expression  may be  enclosed within  braces; doing so  does not  affect the
value of the assignment.  For example, the expressions

    int example = 7+12;

and

    int example = { 7+12 };

are equivalent.

_U_n_i_o_n_s _a_n_d _S_t_r_u_c_t_u_r_e_s
The initialization of a uunniioonn by definition fills only its _f_i_r_s_t member.

To initialize a uunniioonn, use an expression that is enclosed within braces:

    union example_u {
        int member1;
        long member2;
        float member3;
    } = { 5 };

This initializes mmeemmbbeerr11 to five.  That is to say, the uunniioonn is filled with
an iinntt-sized object whose value is five.

To initialize a structure, use a  list of constants or expressions that are
enclosed within braces.  For example:

    struct example_s {
        int member1;
        long member2;
        union example_u member3;
    };

    struct example_s test1 = { 5, 3, 15 };

This  initializes  mmeemmbbeerr11  to  five,  initializes  mmeemmbbeerr22 to  three,  and
initializes the _f_i_r_s_t member of mmeemmbbeerr33 to 15.

_S_t_r_i_n_g_s
To initialize a string pointer,
 use a string literal.

The following initializes a string:

    char string[] = "This is a string";

The length of the character array is 17 characters: one for every character
in the given string literal plus  one for the null character that marks the
end of the string.

If you  wish, you can fix  the length of a character  array.  In this case,
the null  character is appended to  the end of the string  only if there is
room in the array.  For example, the following

    char string[16] = "This is a string";

writes the text into the array  ssttrriinngg, but does not include the concluding
null character because there is not enough room for it.

A pointer  to cchhaarr can  also be initialized  when the pointer  is declared.
For example:

    char *strptr = "This is a string";

initializes ssttrrppttrr  to point to  the first character  in TThhiiss iiss  aa ssttrriinngg.
This declaration automatically allocates exactly enough storage to hold the
given string literal, plus the terminating null character.

_A_r_r_a_y_s
To initialize an  array, use a list of expressions  that is enclosed within
braces.  For example, the expression

    int array[] = { 1, 2, 3 };

initializes  aarrrraayy.  Because  aarrrraayy does  not  have  a  declared number  of
elements, the  initialization fixes its  number of elements  at three.  The
elements of the array are initialized in the order in which the elements of
the initialization  list appear.  For  example, aarrrraayy[00] is  initialized to
one, aarrrraayy[11] to two, and aarrrraayy[22] to three.

If an array has a fixed length and the initialization list does not contain
enough  initializers  to  initialize  every  element,  then  the  remaining
elements  are  initialized  in the  default  manner:  static variables  are
initialized  to zero,  and other  variables  to whatever  happens to  be in
memory.  For example, the following:

    int array[3] = { 1, 2 };

initializes aarrrraayy[00] to one, aarrrraayy[11] to two, and aarrrraayy[22] to zero.

The initialization  of a multi-dimensional array is  something of a science
in itself.  The ANSI Standard defines that the ranks in an array are filled
from right to left.  For example, consider the array:

    int example[2][3][4];

This array contains two groups of three elements, each of which consists of
four   elements.   Initialization   of   this  array   will  proceed   from
eexxaammppllee[00][00][00]  through  eexxaammppllee[00][00][33];  then  from   eexxaammppllee[00][11][00]
through eexxaammppllee[00][11][33]; and so on, until the array is filled.

It is easy  to check initialization when there is  one initializer for each
``slot'' in the array; e.g.,

    int example[2][3] = {
         1,  2,  3,  4,  5,  6
    };

or:

    int example[2][3] = {
        {  1,  2,  3 }, {  4,  5,  6 }
    };

The  situation  becomes more  difficult  when an  array  is only  partially
initialized; e.g.,

    int example[2][3] = {
            { 1 }, { 2, 3 }
    };

which is equivalent to:

    int example[2][3] = {
        { 1, 0, 0 }, { 2, 3, 0 }
    };

As can be seen, braces mark  the end of initialization for a ``cluster'' of
elements within an array.  For example, the following:

    int example[2][3][4] = {
        5, { 1, 2 }, { 5, 2, 4, 3 }, { 9, 9, 5 },
        { 2, 3, 7 } };

is equivalent to entering:

    int example[2][3][4] = {
        { 5, 0, 0, 0 },
        { 1, 2, 0, 0 },
        { 5, 2, 4, 3 },

        { 9, 9, 5, 0 },
        { 2, 3, 7, 0 },
        { 0, 0, 0, 0 }
    };

The  braces end  the initialization  of one cluster  of elements;  the next
cluster is  then initialized.  Any elements within a  cluster that have not
yet been initialized when the brace  is read are initialized in the default
manner.

_S_e_e _A_l_s_o
aarrrraayy, CC llaanngguuaaggee, PPrrooggrraammmmiinngg CCOOHHEERREENNTT, ssttrruucctt, uunniioonn
ANSI Standard, section 3.5.7
