An array is a variable that contains multiple values, each accessible via an index. In C/C++, an array does not remember its size, so this is something we need to remember and keep track of throughout the program.
You declare an array using square brackets ([]), inside which you specify the size of the array. This will give you elements that can be accessed via an index, starting at index 0. As a result, when you declare an array with 4 elements, it will use indexes 0 to 3.
Working with an array usually involves using for loops to iterate over each element of the array, with the for loop’s control variable tracking the valid indexes. You can also access individual array elements directly, and pass the whole array to a parameter. When passing an array to a function or procedure, you will also need to pass on the size of the array so that the procedure knows which indexes it can access.
Here is an illustration showing the declaration and use of an array.
Example
Video Overview and Demonstration
In this video we’ll take a look at the concept of arrays visually, and then walk through some simple code that uses them!