public member function
<string>
const char* data() const;
Get string data
Returns a pointer to an array of characters with the same content as the string.
Notice that no terminating null character is appended (see member
c_str for such a functionality).
The returned array points to an internal location which should not be modified directly in the program. Its contents are guaranteed to remain unchanged only until the next call to a non-constant member function of the string object.
Parameters
none
Return Value
Pointer to an internal array containing the same content as the string.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
// string::data
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int length;
string str = "Test string";
char* cstr = "Test string";
if ( str.length() == strlen (cstr) )
{
cout << "str and cstr have the same length.\n";
length = str.length();
if ( memcmp (cstr, str.data(), length ) == 0 )
cout << "str and cstr have the same content.\n";
}
return 0;
}
|
Output:
str and cstr have the same length.
str and cstr have the same content.
|
Basic template member declaration
( basic_string<charT,traits,Allocator> )
|
const charT* data ( ) const;
|
See also
- string::copy
- Copy sequence of characters from string (public member function)
- string::c_str
- Get C string equivalent (public member function)
- string::assign
- Assign content to string (public member function)