C++20 sections · 1024 units
Open in Course

Declaring Arrays

How to declare an array

Declare an array with type, name, and size: type name[size];. For example, int ages[5]; declares five integers. The size must be a compile-time constant. Examples: double prices[10]; creates ten doubles, char letters[26]; creates 26 characters.

Each allocates contiguous memory for all elements. The size must be greater than zero. You can use expressions like int data[3 + 2]; but not runtime variables. The compiler needs the exact size.