To designate the element of an array,
we'll use the following syntax :
the name of the array, followed by, in brackets ( [ ]),
the index of the element that we want to select.
Be careful, the indexes of an array's elements,
are between zero and the array's size minus one,
they begin at zero
and go to the array's size minus one.
If for example, I have an array with five elements,
that contains 4, 3, 2, 7, 1,
the element 4 here,
is the element with the position zero in the array.
If this array is named tab, I'll use tab[0],
to designate this 4.
Then, I have tab[1], here I have tab[2], tab[3], tab[4].
This array has a size of five.
The last element of the array is tab[5 minus one], namely tab[4].
The indexes do go from zero to the array's size minus one.
The first element of the array is therefore indeed designated by tab[0],
and the tenth element of the array is designated by tab[9],
as we begin at zero.
Be also careful with the fact that there is no control at all
if you are in a situation of array overflow.
If for example, you write with the previous example tab[10],
nothing will tell you you have overflowed here the array's size.
So be careful when you write
something like tab[i],
to be sure that i is between zero and the array's size minus one,
or is strictly less than the array's size,
or else you may encounter this fatal error,
that we call segmentation fault,
which indicates a memory access error.
I'm telling you, you may encounter it, because it isn't even guaranteed
that the segmentation fault happens, the error is there, but it isn't even sure
that the program will detect it.
Anyway, if the detection happens,
at that moment, you'll have a segmentation fault,
with here the typical example, that we encounter a lot
in beginner programmers' programs,
which is the following : we declare
a dynamic array, of doubles, which I name v here,
that is initialized to empty,
as there's nothing after the declaration,
so we initialize an empty array,
so this array is strictly empty, there's nothing in it.
It's an array with zero elements.
In general, the programmer who have written a program,
will rarely make the mistake just after it, but the error is still of this type,
he'll write something like v[0] = with an assignment,
to try and put 5.4 in the array,
but as this array is empty, we don't even have a v[0],
v[0] doesn't exist in the empty array,
so here you'll get a segmentation fault.
A common way of accessing the elements of an array,
is to iterate over the array.
In C++, we have three ways of iterating over an array.
Since the new C++ 2011 standard,
we have a way that let's us iterate simply over all the values.
We'll detail it in a moment.
You could also, of course, with the iteration
you have seen until now, the standard for loop,
iterate over the different explicit indexes of the array,
so if I designate these indexes using i,
l'll do a loop iterating on different values for i,
i will begin with the value 0,
and will go for example one by one, till the last element of the array,
the last element, let me remind you, if t is the array's size,
has the index t - 1.
So here we'll do a loop
that goes from 0 to strictly less than the array's size.
The question is how are we going to represent this size,
how are we going to know this size,
I'll detail that to you in a moment, too.
I'll also explain what this new size_t type represents.
Finally, third way, much more advanced,
which will not be presented in this course,
is to use what are called iterators.
Once you want to iterate over an array,
the question you'll ask yourself,
is which of these two previous ways to choose.