C++ Programming banner
Fifth EditionD.S. Malik

Chapter 9


An array in C++ is a block of memory representing a collection of many simple data variables. You declare arrays in C++ by specifying the number of members in the array in [] after the name of the variable in a variable declaration. Arrays in C++ are zero-bounded; that is the index of the first element in the array is 0 and the last element is N-1, where N is the size of the array. It is illegal to refer to an element outside of the array bounds, and your program will crash or have unexpected results, depending on the compiler.

When you declare an array, you may use the assignment operator during the variable declaration to initialize the array. You must include the initialization value or values in braces {}’s. If there is only a single value inside the braces, all elements in the array are initialized to that value. If more than one value is in the braces, separated by commas, the array is initialized beginning with the first element and so on, until the initializing values are assigned. When you run out of values in the initializing list, the remainder of the array is undefined; this is the default value for any array, just like any other variable.

The base address of an array is the address of the first element in computer memory. When you declare an array, the only things about the array that the computer remembers are the name of the array, its base address, the data type of each component, and (possibly) the number of components. Using the base address of the array and the index of an array component, the computer determines the address of a particular component. Arrays can be passed as parameters to functions, but they are passed by reference only.

Character arrays are of special interest, and you process them differently than you process other arrays. C++ provides a set of functions that can be used for C-string manipulation. The header file cstring describes these functions. We often use three of these functions: strcpy (string copy, to copy a C-string into a C-string variable—that is, assignment); strcmp (string comparison, to compare C-strings); and strlen (string length, to find the length of a C-string). Aggregate operations are allowed for C-string input and output.

Since an array can only hold values of one type, you will often find that you have lists of multiple types. To accommodate this, you will need to use “parallel” arrays, which are arrays of the same size that logically work together. It is up to the programmer to ensure that the indices are coordinated between parallel arrays. There is no mechanism in C++ to maintain the parallel nature of the arrays.

Multidimensional arrays allow you to represent grids of data of the same type. A two-dimensional array in C++ is built by using two sets of []’s when you define the array. The first dimension of the array is the row, and the second dimension of the array is the column. The row is always the first dimension in C++, therefore if you add a third dimension, you will have the dimension of rows, the dimension of columns, and then a third dimension. When initializing multidimensional arrays, you use additional sets of {}’s to contain the row elements, then the columns of rows, and any other dimensions.

C++ Programming cover image

D.S. Malik
ISBN-10: 0-5387-9808-4
ISBN-13: 978-0-5387-9808-2