C++ std::string data() Function

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

Definition

In C++, std::string::data() returns a pointer to the string\'s character buffer. The characters are stored contiguously.

Since C++11, data() and c_str() both point at a null-terminated array for reading. Since C++17, non-const data() returns char* so you can write through it carefully. Include <string>.

This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.

Syntax

Call with empty brackets when you need a raw pointer.

C++
const char* p = s.data();
char* w = s.data();  // C++17 non-const
CallWhat it does
s.data()Pointer to the first character of the contiguous buffer (plus a trailing null for C compatibility).

Do not free the pointer. Do not use it after the string is destroyed or after a reallocation. Writing past size() is undefined.

Library declarations
C++
const char* data() const noexcept;
char* data() noexcept; // C++17

Parameters

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

Return Value

Return ValueDescription
pointerconst char* on const strings; char* on non-const since C++17.

C++ Examples

Example 1 — Basic data()

Print characters through the data pointer.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "Go";
    const char* p = s.data();
    std::cout << p[0] << p[1] << '\n';
    return 0;
}

How It Works

  1. data() points at the first character.
  2. Indexing the pointer reads the same bytes as the string.
  3. Printing two characters shows Go.

Example 2 — Print Like a C-String

Pass data() to a C-style print.

C++
#include <cstdio>
#include <string>

int main() {
    std::string s = "hello";
    std::printf("%s\n", s.data());
    return 0;
}

How It Works

  1. printf("%s") needs a null-terminated C-string.
  2. data() provides that buffer for reading.
  3. c_str() would also work here.

Example 3 — Write Through data() (C++17)

Uppercase the first character via a writable pointer.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "cpp";
    char* p = s.data();
    p[0] = 'C';
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. Non-const data() returns a writable char* (C++17).
  2. Changing p[0] changes the string storage.
  3. The string now prints Cpp.

Frequently Asked Questions

A pointer to the string’s contiguous character array.
For reading, both give a null-terminated buffer. c_str() is always const char*. data() on non-const strings is char* since C++17.
No. The string owns the memory.
After the string is destroyed, moved-from, or reallocates its buffer.
No. Only indices 0 .. size()-1 are your characters; keep size() consistent.

Next: empty()

Keep going through the string functions. Check whether a std::string holds no characters.

empty() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

15 people found this page helpful