3.5.2. Accessing the Elements of an Array
As with the library vector
and string
types, we can use a range for
or the subscript operator to access elements of an array. As usual, the indices start at 0. For an array of ten elements, the indices are 0 through 9, not 1 through 10.
When we use a variable to subscript an array, we normally should define that variable to have type size_t
. size_t
is a machine-specific unsigned type that is guaranteed to be large enough to hold the size of any object in memory. The size_t
type is defined in the cstddef
header, which is the C++ version of the stddef.h
header from the C library.
With the exception that arrays are fixed size, we use arrays in ways that are similar to how we use vector
s. For example, we ...
Get C++ Primer, Fifth 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.