< cpp‎ | string‎ | basic string
const CharT* data() const;
CharT* data();
(2) (since C++17)

Returns pointer to the underlying array serving as character storage. The pointer is such that the range [data(); data() + size()) is valid and the values in it correspond to the values stored in the string.

The returned array is not required to be null-terminated.

If empty() returns true, the pointer is a non-null pointer that should not be dereferenced.
(until C++11)

The returned array is null-terminated, that is, data() and c_str() perform the same function.

If empty() returns true, the pointer points to a single null character.
(since C++11)

The pointer obtained from data() may be invalidated by:

1) Modifying the character array accessed through data has undefined behavior.
2) Modifying the past-the-end null terminator stored at data()+size() has undefined behavior.


[edit] Parameters


[edit] Return value

A pointer to the underlying character storage.

data()[i] == operator[](i) for every i in [0, size()). (until C++11)
data() + i == &operator[](i) for every i in [0, size()]. (since C++11)

[edit] Complexity


[edit] Exceptions

(none) (until C++11)
noexcept specification:  
(since C++11)

[edit] Example

#include <algorithm>
#include <cassert>
#include <cstring>
#include <string>
int main()
  std::string const s("Emplary");
  assert(s.size() == std::strlen(;
  assert(std::equal(s.begin(), s.end(),;
  assert(std::equal(, + s.size(), s.begin()));
  assert(0 == *( + s.size()));

[edit] See also

accesses the first character
(public member function) [edit]
accesses the last character
(public member function) [edit]
returns a non-modifiable standard C character array version of the string
(public member function) [edit]