The String Functions (STRING.H)

Chapter 9 - Arrays

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Accessing Array Elements
A variable declaration usually reserves one or more cells in internal memory and, through a lookup table, associates a name with the cell or cells that you can use to access the cells. For example, the following definition reserves only one integer-sized cell in internal memory and associates the name ivideo_tapes with that cell. See the top of Figure 9-1.
Figure 9-1: Storing variables and arrays in memory
int ivideo_tapes;
On the other hand, the next definition reserves seven contiguous cells in internal memory and associates the name ivideo_library with the seven cells. See the bottom of Figure 9-1.
int ivideo_library[7];
Since all array elements must be of the same data type, each of the seven cells in the array ivideo_library can hold one integer.
Consider the difference between accessing the single cell associated with the variable ivideo_tapes and the seven cells associated with the array ivideo_library. To access the cell associated with the variable ivideo_tapes, you simply use the name ivideo_tapes. For the array ivideo_library, you must specify an index to indicate exactly which cell among the seven you wish to access. The following statements designate the first cell, the second cell, the third cell, and so on, up to the last cell of the array:
ivideo_library[0];
ivideo_library[1];
ivideo_library[2];
ivideo_library[3];
     .
     .
     .
ivideo_library[6];
When accessing an array element, the integer enclosed in the square brackets is the index, which indicates the offset, or the distance between the cell to be accessed and the first cell.
One of the principal mistake novice programmers make has to do with the index value used to reference an array’s first element. The first element is not at index position [1]; instead, it is [0] since there is zero distance between the first element and itself. The third cell has an index value of 2 because its distance from the first cell is 2.
When working with arrays, you can use the square brackets in two quite different ways. When you are defining an array, the number of cells is specified in square brackets:
int ivideo_library[7];
However, when you are accessing a specific array element, you use the array’s name together with an index enclosed in square brackets:
ivideo_library[3];
Assuming the previous declaration for the array ivideo_library, the following statement is logically incorrect:
ivideo_library[7] = 53219;
It is not a legal reference to a cell under the name ivideo_library. The statement attempts to reference a cell that is a distance of 7 from the first cell, that is, the eighth cell. Because there are only seven cells, this is an error. It is up to you to ensure that index expressions remain within the array’s bounds.
Examine the following declarations:
#define iDAYS_OF_WEEK 7

int ivideo_library[iDAYS_OF_WEEK];
int iweekend = 1;
int iweekday = 2;
Take a look at what happens with this set of executable statements:
ivideo_library[2];
ivideo_library[iweekday];
ivideo_library[iweekend + iweekday];
ivideo_library[iweekday - iweekend];
ivideo_library[iweekend - iweekday];
The first two statements both reference the third element of the array. The first statement accomplishes this with a constant value expression, while the second statement uses a variable. The last three statements demonstrate that you can use expressions as subscripts, as long as they evaluate to a valid integer index. Statement three has an index value of 3 and references the fourth element of the array. The fourth statement, with an index value of 1, accesses the second element of the array. The last statement is illegal because the index value –1 is invalid.
It is also possible to access any element in an array without knowing how big each element is. For example, suppose you want to access the third element in ivideo_library, an array of integers. Remember from Chapter 6 that different systems allocate different size cells to the same data type. On one computer system, an integer might occupy 2 bytes of storage, whereas on another system, an integer might occupy 8 bytes of storage. On either system, you can access the third element as ivideo_library[2]. The index value indicates the number of elements to move, regardless of the number of bits allocated.
This offset addressing holds true for other array types. On one system, integer variables might require twice as many bits of storage as does a char type; on another system, integer variables might require four times as many bits as do character variables. Yet to access the fourth element in either an array of integers or an array of characters, you would use an index value of 3.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net