std::string::data() in C++

06 Jul, 2017
The data() function writes the characters of the string into an array. It returns a pointer to the array, obtained from conversion of string to the array. Its Return type is not a valid C-string as no ‘\0’ character gets appended at the end of array.Syntax:
const char* data() const;
char* is the pointer to the obtained array.
Parameters : None

std::string::data() returns an array that is owned by the string. Thus, the caller must not modify or free the memory.Let’s take same example where ptr points to final array.ptr[2] = 'a';
this will raise an error as ptr points to an array that
is owned by the string, in other words ptr is now pointing
to constant array and assigning it a new value is now not allowed.

ptr[2] = 'a';
this will raise an error as ptr points to an array that
is owned by the string, in other words ptr is now pointing
to constant array and assigning it a new value is now not allowed.

Return value of data() is valid only until the next call of a non-constant member function for the same string.Explanation :Suppose, str be the original string which is required to be converted in an array// converts str in an array pointed by pointer ptr.
const char* ptr = str.data(); 

// Bad access of str after modification
// of original string to an array
str += "hello"; // invalidates ptr

// Now displaying str with reference of ptr
// will give garbage value
cout << ptr; // Will give garbage value

// converts str in an array pointed by pointer ptr.
const char* ptr = str.data(); 

// Bad access of str after modification
// of original string to an array
str += "hello"; // invalidates ptr

// Now displaying str with reference of ptr
// will give garbage value
cout << ptr; // Will give garbage value

// CPP code to illustrate// std::string::data()   #include <iostream>#include <string>#include <cstring>using namespace std;   // Function to demonstrate data() void dataDemo(string str1){    // Converts str1 to str2 without appending    // '/0' at the end    const char* str2;    str2 = str1.data();          cout << "Content of transformed string : ";    cout << str2 << endl;       cout << "After data(), length: ";    cout << strlen(str2);            }          // Driver codeint main(){    string str("GeeksforGeeks");       cout << "Content of Original String : ";    cout << str << endl;    cout << "Length of original String : ";    cout << str.size() << endl;         dataDemo(str);       return 0;}
Output:
Content of Original String : GeeksforGeeks
Length of original String : 13
Content of transformed string : GeeksforGeeks
After data(), length: 13

Here, we can easily notice, both contents and length of original string and transformed array are same.This article is contributed by Sakshi Tiwari. If you like GeeksforGeeks (We know you do!) and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
cpp-string
cpp-strings-library
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
Friend class and function in C++
std::string class in C++
Pair in C++ Standard Template Library (STL)
Queue in C++ Standard Template Library (STL)
Unordered Sets in C++ Standard Template Library
List in C++ Standard Template Library (STL)
std::find in C++
Inline Functions in C++