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
Call
What 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.