A quick note about arrays is sufficient for the work on strings discussed in this chapter. Arrays are thoroughly discussed later, and in what follows just the bare minimum is presented in order to feel comfortable with the indexing that strings use.
An array consists of a fixed sized collection of elements of the same type.
The array elements are found one after another in memory, and can be accessed by an index.
The element that comes first in an array has an index of 0.
#include <iostream>
int main()
{
int values[4] = {10, 20, 30, 40};
std::cout << values[0] << '\n';
std::cout << values[3] << '\n';
return 0;
}
An ordinarily created array has allocated space for a predetermined number of elements. An array with space for four integers has just that many integer positions: 0, 1, 2, and 3.
0 is the first index, 3 is the last. Index 4 is empty.
Trying to access an index outside the array's range will have unknown effects.
The size of an ordinary array cannot be changed after its creation. Strings, however, can grow or shrink at will.
What is important about both the arrays and the strings is the fact that they provide a means by which to access the individual elements. This is achieved using square brackets.
This is pretty much the only thing about arrays that is important to know for this chapter. Everything about arrays, which has to do with pointers, the size of arrays, or which ways can be used with them, or how array parameters are passed into functions, will be covered in another chapter.
0 comments
No comments yet. Be the first.