An array is an aggregate data type whose individual data items are called elements and are accessed with an index or subscript number. Arrays order their elements, so an index number always refers to the same element. Computers can move the array elements around in a program as a group using the array name, and they can access individual array elements with the array name and an index number.
![]() |
![]() |
| (a) | (b) |
C++ arrays are said to be zero-indexed. Therefore, for an array with n elements, the first element is at index location 0 and the last is at location n-1. Consequently, all valid index values must be in the range 0 to n-1. Stated mathematically, 0 ≤ index ≤ n-1 or 0 ≤ index < n.
C++ does not check that index values are within the valid range, and an incorrect index value might crash the program, return a meaningless value, or corrupt other data located near the array in memory.
| Arrays | Structures | |
|---|---|---|
| Contained Data | All elements are the same type | Fields or elements may be different types |
| Specification | N/A | struct part
{
int code;
double cost;
}; |
| Definition | int test[10];"10" is the size of the array |
part widget; |
| Element Access | By index or subscripttest[5]brackets are the selection operator & "5" is the index value |
By namewidget.cost"." (i.e., dot) is the selection operator |