Arrays


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[10]; // Declare an array of ten integers. int x; nArray[0] = 23; // The first element in the array starts at // the offset 0 nArray[9] = 21; // The last element in the array starts at // the offset 9 x = nArray[0];

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.




Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
EAN: N/A
Year: 2003
Pages: 208

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