Topic 33 of 91 6. C++ Arrays and Strings Interactive Tutorial

Arrays in C++: Declaring, Initializing, Accessing, and Updating Elements

15 mins read Last updated: 26 Sep 2026 22,300 views

Direct Answer & Core Concept

An array in C++ is a contiguous sequence of elements of the same data type stored in contiguous memory. Arrays are declared with fixed size (Type arr[N]), indexed from 0 to N-1, and provide O(1) random element access via index calculation address = base + (index * sizeof(Type)). Modern C++ recommends std::array for zero-overhead fixed arrays with safe .at() bounds checking.

Detailed Explanation & Mechanics

1. Array Memory Layout:

main.cpp
Run Online
CONTIGUOUS MEMORY STORAGE:
Index:    arr[0]   arr[1]   arr[2]   arr[3]
Offset:    +0B      +4B      +8B     +12B   (for 4-byte integers)

2. C-Style Array vs `std::array`:

  • int arr[5]; (Raw array: decays to pointer, lacks size method, no bounds checking).
  • std::array arr; (STL wrapper: knows its size .size(), iterable, zero runtime overhead).

Runnable Code Example: Arrays in C++

C++20 / C++23
main.cpp
Run Online
#include <iostream>
#include <array>

int main() {
    // Modern std::array
    std::array<int, 5> scores = {92, 85, 78, 95, 88};

    scores[0] = 96; // Direct indexing

    std::cout << "Array Size: " << scores.size() << "
";
    std::cout << "Elements: ";
    for (int score : scores) {
        std::cout << score << " ";
    }
    std::cout << "
";

    // Safe bounds-checked access
    try {
        std::cout << "Element at index 2: " << scores.at(2) << "
";
        // scores.at(10); // Throws std::out_of_range
    } catch (const std::out_of_range& e) {
        std::cerr << "Out of range error caught: " << e.what() << "
";
    }
    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: Why does accessing an out-of-bounds array index in C-style arrays cause undefined behavior?

A: C++ does not perform runtime bounds checks on the `[]` operator for maximum speed. Accessing `arr[10]` in an array of size 5 reads or writes adjacent memory, causing data corruption or segmentation faults.
Q

Q: What is the benefit of std::array over built-in C-style arrays?

A: `std::array` provides a standard STL container interface (`size()`, `begin()`, `end()`, `at()`), does not decay to a pointer implicitly, can be passed and returned by value, and has identical memory layout with zero runtime performance cost.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

1. Replace raw C-style arrays with `std::array` for fixed sizes or `std::vector` for dynamic sizes. 2. Use `.at(index)` when bounds validation is critical to throw `std::out_of_range`. 3. Iterate with range-based for loops `for (const auto& x : arr)` to eliminate off-by-one errors.
We value your privacy & transparency

OperateTools uses cookies and local storage to ensure basic tool functionality, preserve preferences, and analyze aggregated traffic. We never sell your personal data or index your document files. Learn more in our Privacy Policy.