Ad space (banner)
Lesson 7 / 20

Working with Arrays

This lesson covers the basics of using an array in C++. It's for anyone searching "C++ array usage."

An array lets you hold multiple values of the same type together. Declare one in the form type arrayName[size] = {value1, value2, ...};.

The example creates an array, int scores[3] = {80, 95, 70};, and totals every element in a for loop. You access an array's elements by index, like scores[0].

A common early mistake is not realizing an array's index starts at 0. An array with 3 elements runs from scores[0] to scores[2], and accessing scores[3] is an out-of-bounds error (undefined behavior).

In real projects, arrays are used for handling a fixed amount of data together, while data whose size changes is typically better suited to a vector, covered in a later lesson.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C++ directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)