Home Segments Index Top Previous Next

374: Mainline

To define a global array—one introduced outside of the definition of any function—you need to supply an array definition that tells C++ the name of the array, the type of objects that the array is to contain, the number of dimensions the array has, and the size of each dimension. The C++ compiler uses array definitions to calculate how much storage to allocate for the array.

The following array definition, for example, tells the C++ compiler to allocate memory for a one-dimensional array containing five integers of type int, each of which represents a distance to be traveled by a train:

 *-- Specifies that the array contains integer objects 
 | 
 |   *-- Specifies that the name of the array is distances 
 |   | 
 |   |        *-- Specifies that the array contains five objects 
 |   |        | 
 v   v        v 
int distances[5];