Array Elements

You access each of the array elements by referring to an offset from the array name followed by subscript within the braces. Array elements are counted from 0; therefore, the first array element is arrayName[0]. In the LongArray example, LongArray[0] is the first array element, LongArray[1] the second, and so forth.

This can be somewhat confusing. The array SomeArray[3] has three elements: SomeArray[0], SomeArray[1], and SomeArray[2]. More Generally, SomeArray[n] has n elements that are numbered SomeArray[0] through SomeArray[n-1].

Therefore, LongArray[25] is numbered from LongArray[0] through LongArray[24]. Listing 15.1 shows how to declare an array of five integers and fill each with a value.

Listing 15.1. Using an Integer Array ...

Get Sams Teach Yourself C++ in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.