std::basic_string::data - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::basic_string::data

From cppreference.com
< cpp‎ | string‎ | basic string

const CharT* data() const; |  |

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:

- Passing a non-const reference to the string to any standard library function, or
- Calling non-const member functions on the string, excluding `operator[]()`, at(), front(), back(), begin(), end(), rbegin(), rend().

Modifying the character array accessed through `data` is undefined behavior.

### [edit] Parameters

(none)

### [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

Constant.

### [edit] Exceptions

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

### [edit] Example

Run this code

    #include <algorithm>
    #include <cassert>
    #include <cstring>
    #include <string>

    int main()
    {
      std::string const s("Emplary");
      assert(s.size() == std::strlen(s.data()));
      assert(std::equal(s.begin(), s.end(), s.data()));
      assert(std::equal(s.data(), s.data() + s.size(), s.begin()));
      assert(0 == *(s.data() + s.size()));
    }

### [edit] See also

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

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/data&oldid=70317"
Category:

- unconditionally noexcept