Arrays

Arrays contain a linear table of values, which must all be of the same type, called the base type. These values are called the array's elements, and any element can be specified by its position in the array, called its index. In this section we will discuss C++'s built-in arrays, together with typical array operations like copying and searching. The next section will then look at the standard containers, which are 'intelligent' arrays.

Arrays as Tables of Values

A C++ array declaration reserves space for a number of elements. For instance, the following declarations create an array of ints and an array of chars:

;> int arr1 [10];
;> char arr2 [10];
;> sizeof(arr1);
(int) 40
;> sizeof(arr2);
(int) 10

You can see in this example that in ...

Get C++ By Example: UnderC Learning 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.