< previous page page_602 next page >

Page 602
The value 25 is called an initializer. You also can initialize an array in its declaration, and the initializer has a special syntax. You specify a list of initial values for the array elements, separate them with commas, and enclose the list within braces:
int age[5] = {23, 10, 16, 37, 12};
In this declaration, age[0] is initialized to 23, age[1] is initialized to 10, and so on. There must be at least one initial value between the braces. If you specify too many initial values, you get a syntax error. If you specify too few, the remaining array elements are initialized to zero. It's always a good idea to specify exactly the right number of initial values.
Arrays follow the same rule as simple variables about the time(s) at which initialization occurs. A static array (one that is either global or declared as static within a block) is initialized once only, when control reaches its declaration. An automatic array (one that is local and not declared as static) is reinitialized each time control reaches its declaration.
An interesting feature of C++ is that you are allowed to omit the size of an array when you initialize it in a declaration:
float temperature[] = {0.0, 112.37, 98.6};
The compiler figures out the size of the array (here, 3) according to how many initial values are listed. In general, this feature is not particularly useful. In Chapter 12, though, we'll see that it can be convenient for initializing certain kinds of char arrays called strings.
(Lack of) Aggregate Array Operations
Some programming languages allow aggregate operations on arrays. An aggregate operation is one that manipulates the array as an entire unit.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Aggregate Operation An operation on a data structure as a whole, as opposed to an operation on an individual component of the data structure.
C++ does not provide aggregate operations on arrays. If x and y are declared as

 
< previous page page_602 next page >