Fixed-Size Arrays and C-Style Arrays Terminology Reference
This reference provides an overview of std::array and C-style array terminology you'll encounter in this chapter. Use it as a quick lookup guide.
std::array Basics
| Term |
Definition |
Example |
| std::array |
Fixed-size array container from standard library |
std::array<int, 5> arr; |
| Fixed-Size Array |
Array with size known at compile time |
Size cannot change after creation |
| Aggregate Type |
Type that can use aggregate initialization |
Structs and arrays |
| Template Parameters |
Type and size of std::array |
<int, 5> in std::array<int, 5> |
std::array Size and Access
| Term |
Definition |
Example |
| Array Length |
Number of elements in array |
Second template parameter |
| Compile-Time Size |
Size must be constant expression |
constexpr int size{10}; |
| Zero-Length Array |
std::array<T, 0> is valid but empty |
Legal but not useful |
| Array Bounds |
Valid indices from 0 to size-1 |
Accessing outside is undefined behavior |
std::array Operations
| Term |
Definition |
Example |
| Array Indexing |
Accessing element with [] |
arr[0] gets first element |
| at() Method |
Bounds-checked element access |
arr.at(0) throws if invalid |
| Array Assignment |
Copying all elements |
arr1 = arr2; |
| Array Comparison |
Element-wise equality check |
arr1 == arr2 |
Passing std::array
| Term |
Definition |
Example |
| Pass by Reference |
Passing array without copying |
void func(std::array<int, 5>& arr) |
| Pass by const Reference |
Read-only array parameter |
void func(const std::array<int, 5>& arr) |
| Template Function for Arrays |
Function accepting any array size |
template<size_t N> void func(std::array<int, N>&) |
| Array Size Deduction |
Template parameter deduces array size |
Compiler determines N automatically |
std::array Initialization
| Term |
Definition |
Example |
| Aggregate Initialization |
Initialize with braced list |
std::array<int, 3> arr{1, 2, 3}; |
| Partial Initialization |
Fewer initializers than elements |
std::array<int, 5> arr{1, 2}; rest are 0 |
| Brace Elision |
Omitting inner braces |
std::array<int, 3> arr{1, 2, 3} not {{1,2,3}} |
| CTAD |
Class Template Argument Deduction |
std::array arr{1, 2, 3}; deduces type/size |
Arrays of Complex Types
| Term |
Definition |
Example |
| Array of Structs |
std::array containing struct elements |
std::array<Point, 10> points; |
| Nested Braces |
Braces for array and struct initialization |
{Point{1,2}, Point{3,4}} |
| Memberwise Initialization |
Initializing struct members in array |
Each element initialized separately |
| Array of Class Objects |
Array holding class instances |
Constructor called for each element |
Reference Wrappers
| Term |
Definition |
Example |
| std::reference_wrapper |
Wrapper allowing references in containers |
std::array<std::reference_wrapper<int>, 3> |
| Array of References |
Using reference_wrapper for reference arrays |
Cannot have array of actual references |
| get() Method |
Accessing underlying reference |
ref.get() returns actual reference |
| Implicit Conversion |
reference_wrapper converts to reference |
Can use like normal reference |
std::array and Enumerations
| Term |
Definition |
Example |
| Enum as Index |
Using enum to index array |
arr[Color::Red] |
| Compile-Time Array Size |
Using enum count for array size |
std::array<int, static_cast<int>(Color::Count)> |
| Type-Safe Indexing |
Enum provides meaningful index names |
Better than magic numbers |
| Enum Underlying Value |
Integer value used as array index |
Automatically converted to size_t |
C-Style Arrays
| Term |
Definition |
Example |
| C-Style Array |
Built-in array from C language |
int arr[5]; |
| Array Declaration |
Declaring array with size in brackets |
type name[size]; |
| Subscript Syntax |
Using brackets for index |
arr[0] |
| Array Variable |
Name representing array |
Actually represents address of first element |
Array Decay
| Term |
Definition |
Example |
| Array Decay |
Conversion of array to pointer |
int arr[5] becomes int* in expressions |
| Implicit Conversion |
Automatic array-to-pointer conversion |
Happens in most contexts |
| Decay to Pointer |
Array name becomes pointer to first element |
arr becomes &arr[0] |
| Loss of Size Information |
Pointer doesn't know original array size |
Size information is lost |
Pointer Arithmetic
| Term |
Definition |
Example |
| Pointer Arithmetic |
Math operations on pointers |
ptr + 1 moves to next element |
| Pointer Increment |
Moving pointer forward |
++ptr points to next element |
| Pointer Decrement |
Moving pointer backward |
--ptr points to previous element |
| Pointer Offset |
Adding integer to pointer |
ptr + 3 points 3 elements ahead |
| Pointer Difference |
Subtracting pointers |
ptr2 - ptr1 gives element count between |
Pointer Subscripting
| Term |
Definition |
Example |
| Pointer Subscript |
Using [] on pointer |
ptr[2] same as *(ptr + 2) |
| Array Index Equivalence |
arr[i] is *(arr + i) |
Subscript is syntactic sugar |
| Pointer Dereference |
Accessing value pointer points to |
*ptr gets value |
| Address Arithmetic |
Computing element addresses |
Each element at base + i * sizeof(T) |
C-Style Strings
| Term |
Definition |
Example |
| C-Style String |
Null-terminated character array |
char str[] = "Hello"; |
| Null Terminator |
\0 character ending string |
Marks end of string |
| String Literal |
Text in quotes creating const char array |
"Hello" is string literal |
| Character Array |
Array of char type |
May or may not be null-terminated |
String Operations
| Term |
Definition |
Example |
| std::strlen |
Function returning string length |
std::strlen(str) excludes null terminator |
| std::strcpy |
Copy string to another buffer |
std::strcpy(dest, src); |
| std::strcat |
Concatenate strings |
std::strcat(dest, src); |
| std::strcmp |
Compare two strings |
Returns 0 if equal |
String Literals
| Term |
Definition |
Example |
| String Literal |
Constant text stored in read-only memory |
"Hello, World!" |
| const char* |
Type of string literal |
Pointer to const characters |
| String Constant |
Non-modifiable character array |
Cannot modify literal contents |
| Literal Pool |
Memory area where literals stored |
Managed by compiler |
Multidimensional Arrays
| Term |
Definition |
Example |
| Multidimensional Array |
Array of arrays |
int grid[3][4]; is 3 rows, 4 columns |
| 2D Array |
Two-dimensional array (rows and columns) |
arr[row][col] |
| Row-Major Order |
Elements stored row by row |
C++ default layout |
| Nested Arrays |
Array whose elements are arrays |
Each row is an array |
| Jagged Array |
Array of arrays with different sizes |
Not built-in, use vector of vectors |
Multidimensional std::array
| Term |
Definition |
Example |
| Nested std::array |
std::array of std::array |
std::array<std::array<int, 4>, 3> |
| 2D std::array |
Fixed-size 2D container |
Type-safe alternative to C-style 2D arrays |
| Array of Arrays |
Outer array contains inner arrays |
Each element is another std::array |
| Type Safety |
std::array provides bounds checking |
Safer than C-style multidimensional arrays |