- 01Getting Started
- 02Your First Output (cout)
- 03Variables and Types (int/double/string/bool)
- 04Conditionals (if statements)
- 05Loops (for statements)
- 06Writing a Function
- 07Working with Arrays
- 08Loops (while statements)
- 09Pointer Basics
- 10References
- 11Classes (Object-Oriented Programming)
- 12Constructors and Destructors
- 13Inheritance
- 14Using vector (the Standard Library)
- 15String Manipulation (string)
- 16Structs (struct)
- 17Error Handling (try/catch)
- 18The Ternary Operator
- 19The switch Statement
- 20[Applied] Build a Mini Inventory Management System
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.
๐งช 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).
