std::basic_string_view::data - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::basic_string_view::data

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

constexpr const_pointer data() const; |  | (since C++17)

Returns a pointer to the underlying character array. The pointer is such that the range [data(); data() + size()) is valid and the values in it correspond to the values of the view.

### [edit] Parameters

(none)

### [edit] Return value

A pointer to the underlying character array.

### [edit] Exceptions

`noexcept` specification:
noexcept

### [edit] Complexity

Constant.

### [edit] Notes

Unlike std::basic_string::data() and string literals, `data()` may return a pointer to a buffer that is not null-terminated. Therefore it is typically a mistake to pass `data()` to a routine that takes just a `const CharT*` and expects a null-terminated string.

### [edit] Example

Run this code

    #include <iostream>
    #include <cstring>
    #include <cwchar>
    #include <string>
    #include <string_view>
    int main()
    {
        std::wstring_view wcstr_v = L"xyzzy";
        std::cout << std::wcslen(wcstr_v.data()) << '\n';
        // OK: the underlying character array is null-terminated

        char array[3] = {'B', 'a', 'r'};
        std::string_view array_v(array, sizeof array);
        // std::cout << std::strlen(array_v.data()) << '\n';
        // error: the underlying character array is not null-terminated

        std::string str(array_v.data(), array_v.size()); // OK
        std::cout << std::strlen(str.data()) << '\n';
        // OK: the underlying character array of a std::string is always null-terminated
    }

Output:

    5
    3

### [edit] See also

front | accesses the first character (public member function) [edit]
back | accesses the last character (public member function) [edit]

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

- unconditionally noexcept

Hidden categories:

- Pages with unreviewed unconditional noexcept template
- Pages with unreviewed noexcept template