An array is a collection of data-storage locations, each of which holds the same type of data—such as all integers or all doubles. Arrays are very useful when you want to represent a collection of values, such as the number of days in each month or the names of company employees. Each storage location is called an element of the array. Elements of the array are accessed by referring to an offset from the array name. The array elements start at zero and continue up to one less than the array bound:
int nArray; // Declare an array of ten integers. int x; nArray = 23; // The first element in the array starts at // the offset 0 nArray = 21; // The last element in the array starts at // the offset 9 x = nArray;
Writing past the end of the array is a serious problem and the cause of many bugs in C++ programs. When accessing an array element, the compiler calculates the offset from the start of the array. If you’re writing to an array element and you give an invalid index so that the calculated offset is beyond the end of the array, the compiler will overwrite whatever happens to be in that memory location.