Initialization

From RAD Studio
Jump to: navigation, search

Go Up to Declaration Syntax Index

Initializers set the initial value that is stored in an object (variables, arrays, structures, and so on). If you don't initialize an object, and it has static duration, it will be initialized by default in the following manner:

  • To zero if it is an arithmetic type
  • To null if it is a pointer type

Note: If the object has automatic storage duration, its value is indeterminate.

Syntax for initializers


initializer
	= expression
	= {initializer-list} <,>}
	(expression list)
initializer-list
	expression
	initializer-list, expression
	{initializer-list} <,>}

Rules governing initializers

  • The number of initializers in the initializer list cannot be larger than the number of objects to be initialized.
  • The item to be initialized must be an object (for example, an array).
  • For C (not required for C++), all expressions must be constants if they appear in one of these places:
    • In an initializer for an object that has static duration.
    • In an initializer list for an array, structure, or union (expressions using sizeof are also allowed).
  • If a declaration for an identifier has block scope, and the identifier has external or internal linkage, the declaration cannot have an initializer for the identifier.
  • If a brace-enclosed list has fewer initializers than members of a structure, the remainder of the structure is initialized implicitly in the same way as objects with static storage duration.

Scalar types are initialized with a single expression, which can optionally be enclosed in braces. The initial value of the object is that of the expression; the same constraints for type and conversions apply as for simple assignments.

For unions, a brace-enclosed initializer initializes the member that first appears in the union's declaration list. For structures or unions with automatic storage duration, the initializer must be one of the following:

  • An initializer list (as described in Arrays, structures, and unions).
  • A single expression with compatible union or structure type. In this case, the initial value of the object is that of the expression.

Arrays, structures, and unions

You initialize arrays and structures (at declaration time, if you like) with a brace-enclosed list of initializers for the members or elements of the object in question. The initializers are given in increasing array subscript or member order. You initialize unions with a brace-enclosed initializer for the first member of the union. For example, you could declare an array days, which counts how many times each day of the week appears in a month (assuming that each day will appear at least once), as follows:

 int days[7] = { 1, 1, 1, 1, 1, 1, 1 }

The following rules initialize character arrays and wide character arrays:

  • You can initialize arrays of character type with a literal string, optionally enclosed in braces. Each character in the string, including the null terminator, initializes successive elements in the array. For example, you could declare
char name[] = { "Unknown" };
which sets up an eight-element array, whose elements are 'U' (for name[0]), 'n' (for name[1]), and so on (and including a null terminator).
  • You can initialize a wide character array (one that is compatible with wchar_t) by using a wide string literal, optionally enclosed in braces. As with character arrays, the codes of the wide string literal initialize successive elements of the array.

Here is an example of a structure initialization:

 struct mystruct {
    int i;
    char str[21];
    double d;
    } s = { 20, "Borland", 3.141 };

Complex members of a structure, such as arrays or structures, can be initialized with suitable expressions inside nested braces.

See Also