C++ string data() Function
Example
Get a pointer to the characters of a string:
string txt = "Hello";
const char* ptr = txt.data();
cout << ptr << "\n";
cout << ptr[1];
Definition and Usage
The data() function returns a pointer to the array where the string stores its characters.
In C++11 and later, data() works the same as c_str(): the characters end with a null character (\0).
The data function is defined in the <string> header file.
Note: You cannot change the characters through the returned pointer. The pointer only works until the string is changed.
Syntax
string.data();
Parameter Values
None.
Technical Details
| Returns: | A pointer (const char*) to the first character of the string. |
|---|
Related Pages
Read more about strings in our String Tutorial.