Chapter 7 Arrays 315 7.2 (Web hosting domains) Arrays An array
Chapter 7 Arrays 315 7.2 Arrays An array is a group of contiguous memory locations that all have the same name and the same type. To refer to a particular location or element in the array, we specify the name of the array and the position number (or index or subscript) of the particular element in the array. Figure 7.1 shows an integer array called c. This array contains 12 elements. A program refers to any one of these elements by giving the name of the array followed by the position number of the particular element in square brackets ([]). The first element in every array has position number zero (sometimes called the zeroth element). Thus, the first element of array cis c[0], the second element of array cis c[1], the seventh element of array c is c[6], and, in general, the ith element of array cis c[i-1]. Array names follow the same conventions as other variable names. Formally, the position number in square brackets is called a subscript (or an index). A subscript must be an integer or an integer expression. If a program uses an expression as a subscript, the program evaluates the expression to determine the subscript. For example, if we assume that variable ais 5 and that variable bis 6, then the statement c[ a + b ] += 2; adds 2 to array element c[11]. Note that a subscripted array name is an lvalue it can be used on the left side of an assignment to place a new value into an array element. c[ 0 ] Name of array (Note that all elements of c[ 1 ] this array have the same name, c) c[ 2 ] c[ 3 ] c[ 4 ] c[ 5 ] c[ 6 ] c[ 7 ] c[ 8 ] c[ 9 ] Position number (index or subscript) of the c[ 10 ] element within array c c[ 11 ] -45 6 0 72 1543 -89 0 62 -3 1 6453 78 Fig. 7.1A 12-element array. Fig. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01