C++ std::array data() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::array

Definition

In C++, std::array::data() returns a pointer to the memory where the array keeps its elements. The pointer points to the first element, and all elements sit one after another after it.

This is handy when you must call older C-style code that wants a plain int* and a length. Include <array> to use it.

This is a member of std::array<T, N>, not of a plain C array and not of std::vector. A std::array has a fixed size N that is part of its type, and it lives on the stack like a normal variable.

Syntax

Call data with empty brackets. It takes no arguments.

C++
int* p = nums.data();
const int* q = cnums.data();
CallWhat it does
nums.data()Returns a pointer to the first element. You can read and write through it.
nums.data()[2]Reads the element at index 2 through the pointer.

The pointer is only valid while the array exists. Do not keep it after the array goes out of scope.

Library declarations
C++
T*       data() noexcept;
const T* data() const noexcept;

Parameters

ParameterDescription
Nonedata() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
PointerA pointer to the first element, of type T*. On a const array it is const T*.
Empty arrayFor std::array<T, 0> the pointer value is not specified, so never dereference it.

C++ Examples

Example 1 — Basic data()

Get a pointer and read the first two elements through it.

C++
#include <array>
#include <iostream>

int main() {
    std::array<int, 3> nums = {10, 20, 30};

    int* p = nums.data();

    std::cout << "First: " << *p << '\n';
    std::cout << "Second: " << p[1] << '\n';
    return 0;
}

How It Works

  1. nums.data() returns the address of the first element.
  2. *p reads the value at that address, which is 10.
  3. p[1] reads one element further on, so it prints 20.

Example 2 — Pass to a C-Style Function

Older functions take a pointer and a count. data() and size() fit them perfectly.

C++
#include <array>
#include <iostream>

int sum(const int* values, std::size_t count) {
    int total = 0;
    for (std::size_t i = 0; i < count; ++i) {
        total += values[i];
    }
    return total;
}

int main() {
    std::array<int, 5> nums = {1, 2, 3, 4, 5};

    std::cout << "Sum: " << sum(nums.data(), nums.size()) << '\n';
    return 0;
}

How It Works

  1. The function sum only understands a pointer and a number of items.
  2. nums.data() is the pointer and nums.size() is the count, here 5.
  3. The loop adds 1 + 2 + 3 + 4 + 5 and returns 15.

Example 3 — Write Through the Pointer

The pointer reaches the real array, so changes show up in the array.

C++
#include <array>
#include <iostream>

int main() {
    std::array<int, 3> a = {1, 2, 3};

    int* p = a.data();
    p[2] = 99;

    std::cout << "a[2] = " << a[2] << '\n';
    std::cout << "Same address as &a[0]: "
              << (a.data() == &a[0] ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. p points into the memory owned by a, not to a copy.
  2. p[2] = 99 changes the third element, so a[2] now shows 99.
  3. data() equals &a[0], because both are the address of the first element.

Frequently Asked Questions

It returns a pointer to the first element of the array. The elements are stored one after another, so you can reach the others with p[1], p[2], and so on.
Use it when you call C-style code that wants a pointer and a length. For normal C++ code, at(), [] and iterators are safer.
No. data() returns a plain pointer (T*). begin() returns an iterator. For std::array they point to the same spot, but they are different types.
No. Always pass size() together with it so the other code knows where the array stops.
The pointer becomes invalid (a dangling pointer). Do not use it after the array goes out of scope.

Next: empty()

Check whether the array has zero elements with std::array.

empty() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful