- [Instructor] The primitive array type is commonly called
a C array, because it's inherited from the C language.
A primitive is a type that's defined as part of a language,
as opposed to a derived type
that is defined in a structure or a class.
The array is a primitive type.
Primitives are essential tools for building derived types.
There also an STL class called array,
and this can lead to some confusion.
This lesson is not about the STL class.
I'll cover that later in the STL chapter
and in the companion course, C++ Templates and the STL.
This lesson is about the primitive array type,
or the C array.
An array is a fixed size container of objects
where each object is the same type.
This is a definition for an array of integers.
The value in the square brackets defines
the size of the array.
This array has room for five integers.
Once defined, the size of the array cannot be changed.
At this point, the array is uninitialized,
so each of the five integer values are undefined.
Arrays are indexed with integer values.
The first element is always number zero,
so an array with a size of five will have elements
numbered zero through four.
The value in the square brackets is the index.
This expression assigns a value to the element
at index zero, which is the first element of the array.
Array elements may also be accessed
as if the array were a pointer.
This statement has exactly the same effect
as the previous statement.
This is a common practice,
so you'll need to recognize it when you see it.
This defines an integer pointer
and assigns the address of the array to the pointer.
Notice that you don't need the address of operator
to get an array's address.
This is because an array may be accessed
as if it were a pointer.
This assigns the value two
to the first element of the array.
Because the pointer was initialized to the address
of the array, it is pointing to the first element.
This is the C and C++ operator for incrementing a value.
You can increment the pointer.
and it will point to the second element.
In C++, pointers are strongly typed,
so they know the size of what they point to.
When you increment a pointer,
it always incurrence by the size of the object
or type it is defined with.
So this assigns the value three
to the second element of the array.
This is another common technique in C++.
Here we're incrementing a pointer
and using it at the same time.
It's just a shortcut for the separate increment
and assignment of the previous two statements.
You may also initialize an array using an initializer list.
This both declares the array and assigns the values
one, two, three, four, and five
to the five elements of the array.
This is a new feature in C++ as of C++ 11,
so it may not work on older compilers.
It's very convenient and you'll see it a lot in newer code.
When using the initializer list,
you may omit the size parameter in the square brackets.
The compiler will infer the size of the array
from the initialization.
Here's a working copy of array.cpp
from chapter two of the exercise files.
This is a simple array of five integers.
We'll go ahead and run this.
The array is declared and initialized
with the initializer list.
The for-loop prints all the elements in the array,
we'll cover loops in more detail later in this chapter,
and the printf is used to print each element in the array,
and we'll cover printf in more detail later in this course.
Elements are indexed beginning at zero,
So if I do this...
That accesses element number two, which is the third element
in the array, and so I can print it.
And when I run this, you'll notice that it says X is three,
which is the third element in the array,
index number two, because they start at zero.
We can also update the element to a different value.
And now, when I run this, you'll see that that third value
is now 47, because again, all of number two
is the third value and it's been updated to a new value.
So take some time and experiment with this array
until you feel you understand it well.
The primitive array is a fundamental part
of the C++ language, and you'll see it often.
It's useful as a basis for more complex structures
and classes, as well as on its own.