Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

The C++ array declarator (`[]`)

The `[]` declarator forms a built-in array type. The bound can be part of the array type, but array syntax in a function parameter does not pass a length.

Version: ISO C++23; implementation support may vary

Syntax

T a[N];                  // array of N elements of type T
T a[] = {v1, v2};         // bound inferred as 2 in this definition
extern T a[];             // unknown bound if no prior declaration supplies it
T grid[R][C];             // R elements, each an array of C elements
T* slots[N];              // array of N pointers to T
T (*p)[N] = &a;           // pointer to the whole array
T (&r)[N] = a;            // lvalue reference to the whole array
void f(const T a[N]);    // adjusted to void f(const T* a)

In T a[N], the array declarator ([]) declares N contiguous elements of type T. An explicit N must be a converted constant expression of type std::size_t with a value greater than zero. Runtime-sized arrays in this declarator are a compiler extension, not ISO C++23. In the expression a[i], brackets mean subscripting, not declaration. Access outside indices 0 through N - 1 has undefined behavior.

In int a[]{1, 2}, the initializer determines the bound. By contrast, extern int a[]; without a prior declaration supplying the bound declares an incomplete array type, on which sizeof cannot be used. An automatic local char buf[32]; leaves the elements uninitialized: write them before reading. In char buf[32]{};, aggregate initialization gives each element the value zero. A member char label[8]; is stored inside its object; if used as a C string, one element must hold the terminating \0.

In T* slots[N], the elements are pointers; in T (*p)[N], p itself points to a whole array. T (&r)[N] is an lvalue reference to a whole array. In T* p = a;, the array implicitly converts to a pointer to its first element, with no length information. For a complete array, sizeof a gives the size of the whole array in bytes. With T grid[R][C], the first element is a row of type T[C], so the conversion yields T (*)[C], not T**.

When declaring a parameter, void f(const T a[N]); is adjusted to void f(const T* a);. These spellings declare the same function, not two overloads; N enforces no minimum length, and const applies to the elements. This rule is separate from conversion of an argument at the call site. Pass the pointer with a valid count of accessible elements, or use an array-reference parameter to retain the bound in the type. Do not access elements through such a pointer or reference after the array's lifetime ends.

Examples

#include <cstddef>
#include <iostream>
#include <type_traits>

struct Record {
    char label[8];
    int value;
};

int sum(const Record items[2], std::size_t count) {
    static_assert(std::is_same_v<decltype(items), const Record*>);
    int total = 0;
    for (std::size_t i = 0; i < count; ++i) {
        total += items[i].value;
    }
    return total;
}

template <std::size_t N>
constexpr std::size_t extent(const Record (&)[N]) {
    return N;
}

int main() {
    const Record records[]{{"A", 3}, {"B", 4}};
    char32_t buffer[4]{};
    const auto count = extent(records);
    std::cout << count << ' ' << sum(records, count) << ' '
              << (buffer[0] == U'\0') << '\n';
}

Result

2 7 1

`extent` deduces the record count from the array type. `sum` receives a pointer and a separate count. The output `2 7 1` means two records, a sum of 7, and a true comparison of the first element of `buffer` with the null character.

Related lessons

  • C++ · Lesson 47 · Advanced · Version: 3
  • C++ · Lesson 53 · Advanced · Version: 3
  • C++ · Lesson 56 · Advanced · Version: 3
  • C++ · Lesson 58 · Advanced · Version: 3

Sources