From the course: Complete Guide to C++ Programming Foundations

Unlock this course with a free trial

Join today to access over 24,400 courses taught by industry experts.

Arrays

Arrays

- [Eduardo] Arrays are collections of data. Each element is accessible by an index. Elements are homogeneous, meaning that they are all of the same data type. Arrays are fixed in size, so you may not resize them. And lastly, elements are continuous in memory. That means that the whole array is a sequential block of memory addresses. So, let me show you some arrays. Let's start at line eight with an array of integers where we want to store the number of enemies per level in a run-and-gun video game. The syntax goes like this. First we specify the type, that's int, then comes the name of the array, enemies, and then between square brackets the capacity of the array. We'll use 4. Now let me assign values to each element in the array. Now, this is important. Arrays in C and C++ are indexed from zero to N minus one, where N is the capacity of the array. This is a common source of bugs for beginners, so please try to keep it in mind. To access an element for reading or writing you simply…

Contents