COHERENT manpages
This page displays the COHERENT manpage for initialization [Definition].
List of available manpages
Index
initialization -- Definition
The term initialization refers to setting a variable to its first, or
initial, value.
Rules of Initialization
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 dsin is declared inside a function.
To initialize an object, use the assignment operator `='. The following
sections describe how to initialize different classes of objects.
Scalars
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.
Unions and Structures
The initialization of a union by definition fills only its first member.
To initialize a union, use an expression that is enclosed within braces:
union example_u {
int member1;
long member2;
float member3;
} = { 5 };
This initializes member1 to five. That is to say, the union is filled with
an int-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 member1 to five, initializes member2 to three, and
initializes the first member of member3 to 15.
Strings
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 string, but does not include the concluding
null character because there is not enough room for it.
A pointer to char can also be initialized when the pointer is declared.
For example:
char *strptr = "This is a string";
initializes strptr to point to the first character in This is a string.
This declaration automatically allocates exactly enough storage to hold the
given string literal, plus the terminating null character.
Arrays
To initialize an array, use a list of expressions that is enclosed within
braces. For example, the expression
int array[] = { 1, 2, 3 };
initializes array. Because array 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, array[0] is initialized to
one, array[1] to two, and array[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 array[0] to one, array[1] to two, and array[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
example[0][0][0] through example[0][0][3]; then from example[0][1][0]
through example[0][1][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.
See Also
array,
C language,
Programming COHERENT,
struct,
union
ANSI Standard, §3.5.7







