HP C/iX Reference Manual (31506-90011)
Chapter 3 49
Data Types and Declarations
Initialization
storage duration are initialized at runtime when entering the block that contains the
definition of the object. An initialization of such an object is similar to an assignment
statement.
You can initialize a static object with a constant expression. You can initialize a static
pointer with the address of any previously declared object of the appropriate type plus or
minus a constant.
You can initialize an auto scalar object with an expression. The expression is evaluated at
run-time, and the resulting value is used to initialize the object.
When initializing a scalar type, you may optionally enclose the initializer in braces.
However, they are normally elided. For example,
int i = {3};
is normally specified as
inti=3;
When initializing the members of an aggregate, the initializer is a brace-enclosed list of
initializers. In the case of a structure with automatic storage duration, the initializer may
be a single expression returning a type compatible with the structure. If the aggregate
contains members that are aggregates, this rule applies recursively, with the following
exceptions:
• Inner braces may be optionally elided.
• Members that are themselves aggregates cannot be initialized with a single expression,
even if the aggregate has automatic storage duration.
In ANSI mode, the initializer lists are parsed "top-down;" in non-ANSI mode, they are
parsed "bottom-up." For example,
int q [3] [3] [2] = {
{1},
{2,3},
{4,5,6}
};
produces the following layout:
ANSI Mode Non-ANSI Mode
-
100000 102345
230000 600000
456000 000000
It is advisable to either fully specify the braces, or fully elide all but the outermost braces,
both for readability and ease of migration from non-ANSI mode to ANSI mode.
Because the compiler counts the number of specified initializers, you do not need to specify
the size in array declarations. The compiler counts the initializers and that becomes the
size:
int x[ ] = {1, 10, 30, 2, 45};
This declaration allocates an array of int called x with a size of five. The size is not
specified in the square brackets; instead, the compiler infers it by counting the initializers.