C++ Reference ยท Types and Language Concepts
C-style arrays in C++
Reference for C-style arrays in C++: declaration, initialization, sizeof, array-to-pointer decay, passing to functions, multidimensional arrays and std::array.
A C-style array is a fixed number of objects of one type stored one after another in memory. You declare it as T name[N], where N is a constant expression. Elements are reached with name[i], counted from zero, and nothing checks that i is in range.
Arrays are inherited from C and have odd rules: they cannot be assigned or returned by value, and in most expressions they silently convert ("decay") to a pointer to their first element, losing their size. In new code std::array (fixed size) and std::vector (dynamic size) behave like ordinary values and are almost always the better choice.
int primes[5] = {2, 3, 5, 7, 11}; int n = sizeof primes / sizeof primes[0]; // 5
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
T name[N]; // N elements, indeterminate for local non-class T T name[N] = {a, b, c}; // aggregate initialization; the rest value-initialized T name[] = {a, b, c}; // N deduced from the initializer T name[R][C]; // array of R arrays of C elements T (*p)[N]; // pointer to an array of N T T (&r)[N]; // reference to an array of N T
| Part | Description |
|---|---|
T |
Element type. It must be complete, and cannot be a reference or void. |
N |
A constant expression greater than zero. A runtime size is not standard C++ (GCC accepts it as an extension, which you should avoid). |
name[i] |
Equivalent to *(name + i). No bounds check. |
Returns: sizeof arr is the size in bytes; sizeof arr / sizeof arr[0] or std::size(arr) (C++17) is the element count.
Errors / undefined behavior: reading or writing outside [0, N), calling sizeof on a decayed parameter (it measures a pointer), reading uninitialized elements of a local array, and storing more characters than a char array holds.
Usage notes
- Decay. In a function parameter,
int a[],int a[4]andint* aall meanint*. The size must be passed separately, or captured with a reference-to-array parameter or a template as in the second example. Usestd::span(C++20) for a pointer-plus-length view. - No assignment, no return.
a = b;does not compile for arrays, and a function cannot return one. Wrap the array in astructor usestd::arrayto copy and return by value. - Initialization. A local
int a[5];contains garbage.int a[5] = {};zeroes it. A static or global array is zero-initialized automatically. - Initializer too long is a compile error; too short fills the rest with zeros (or default-constructed objects).
- Out-of-bounds access is undefined behavior: it may crash, silently corrupt a neighbor, or appear to work. Sanitizers (
-fsanitize=address,undefined) catch it in testing. - String literals are
const char[N]arrays including the trailing'\0'. - Multidimensional arrays are arrays of arrays stored row by row. Only the first dimension can decay: a parameter must be written
int m[][3]orint (*m)[3]. new int[n]creates a dynamic array whose size can vary at runtime, but you must rememberdelete[]. Preferstd::vector.- Version notes:
std::sizeandstd::ssizearrived in C++17/C++20;std::to_array(C++20) builds astd::arrayfrom a C array or braced list.