Initializing Arrays


Like other types of variables, arrays can be initialized when they are created. Array initializations require a value for each location that you want to initialize. For example, if you want to initialize an integer array with five elements, you would write a statement similar to the following:

 int thisArray[5] = {42,11,6,0,1}; 


This initialization starts at the beginning of the array and copies values into the array locations. As a result, thisArray[0] gets the value 42, thisArray[1] gets set to 11, and so on.

An interesting thing about array initializations is that they enable you to leave off the size specifier. Here's an example:

 int thisArray[] = {42,11,6,0,1}; 


Notice that there is no size specifier in the array's square brackets. This is not an error. The compiler counts the number of initial values in the initialization and allocates a memory location in the array for each one. Because there are five values in the initialization, the compiler makes thisArray big enough to hold five integers.

Arrays can have both size specifiers and initializations. You can leave off one or the other, but not both. You cannot, for instance, create an array with a statement like this:

 int thisArray[]; 


In this case, thisArray has no size specifier and no initialization values. Therefore, the compiler will not know how many integer elements to allocate for thisArray. It expresses its displeasure by displaying an error message. To fix this statement, you must use a size specifier, a group of initialization values, or both.

Tip

Always remember that arrays must have either a size specifier, a group of initialization values, or both.


To initialize arrays with more than one dimension, you separate the initialization values with braces. The initialization of a 2D array would resemble the following statement:

 int thisArray[2][3] = { {1,2,3}, {2,3,4} }; 


The array thisArray contains two rows, with three integers per row. The initialization shown here has initializations for both rows. They are contained in the outer set of braces (the leftmost and rightmost ones). The initialization values for the two rows are also each in a set of braces. The two sets of initialization values are separated by a comma.

If multidimensional initializations seem confusing, you can arrange them into something that resembles a table. Here's how it's done:

 int thisArray[2][3] = {     {1,2,3},     {2,3,4} }; 


This initialization does the same thing as the previous one. The braces that contain the entire initialization have been put on lines by themselves. The initializations for each row are each on their own lines. The row initialization values are still separated by a comma. The nice thing about this style of initialization is that it looks like a 2D table.

If you're wondering whether you can initialize a 3D array, the answer is yes. Here's an example of how it's done:

 int thisArray[2][3][2] = {     {{1,2},{3,4},{5,6}},     {{2,3},{4,5},{6,7}} }; 


As you can see, this style of initialization gets a bit more complex with each dimension you add.



Creating Games in C++(c) A Step-by-Step Guide
Creating Games in C++: A Step-by-Step Guide
ISBN: 0735714347
EAN: 2147483647
Year: N/A
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net