7.1. Introduction To Arrays

Time: 00:02:16 | Download: Large, Large (CC), Small | Streaming, Streaming (CC) | Slides (PDF)

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.

An image showing the definition of an array: int test[10]. The array type is <kbd>int</kbd>, the name of the array is <kbd>test</kbd>, and its size is 10. An image showing how to access one element in the array named test: test[5].
(a)(b)
Creating and using arrays.
  1. An array definition.
    • An array is a variable, so when programmers create or define an array, the definition must have the two parts required of any variable definition: (a) a data type and (b) a variable name.
    • Unlike simple, non-aggregate data, arrays also have a size or capacity - the number of elements in the array.
    • When the compiler creates an array, it allocates a contiguous block of memory large enough to contain all the array elements without gaps or spaces.
    • Once a programmer defines an array, its size is fixed and cannot change.
    • Each array element is the same type as the array itself.
  2. A single array element.
    • The individual array elements are accessed by an index (also known as a subscript).
    • Index values follow the array name and appear between opening and closing square brackets, called the index operator.
    • Programmers can "index into an array" with a constant, a variable, or an integer-valued expression.
    • The index value must be greater than or equal to zero and less than the array size.
Array elements are variables. In the same way that a ZIP file is a container file that holds other files, an array is a container variable that holds other variables or elements. Programs can use an array element wherever they can use a variable of that type. Specifically, they can use a single array element as an l-value (on the left side of an assignment operator) or as an r-value (on the right side of an assignment operator or wherever an expression is required).
Zero-Indexed

C++ arrays are said to be zero-indexed, which means that the first element in the array is at index value 0. So, valid index values must be in the range 0 to size-1 (i.e., 0 ≤ index ≤ size-1 or 0 ≤ index < size)

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 data near the array in memory.

  Arrays Structs
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 subscript
test[5]
brackets are the selection operator & "5" is the index value
By name
widget.cost
"." (i.e., dot) is the selection operator
Aggregate data: arrays vs. structures. Arrays and structures are different kinds of aggregate data. Nevertheless, programmers create and use them in different ways.