std::string::data() in C++ Last Updated : 06 Jul, 2017 Comments Improve Suggest changes Like Article Like Report 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. 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 CPP // 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 code int 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. If you like GeeksforGeeks (We know you do!) and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected]. Comment More infoAdvertise with us Next Article std::string::data() in C++ S Sakshi Tiwari Improve Article Tags : Misc C++ STL cpp-string cpp-strings-library +1 More Practice Tags : CPPMiscSTL Similar Reads std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. The string class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.String vs Character ArrayStringChar 8 min read std::string::clear in C++ The string content is set to an empty string, erasing any previous content and thus leaving its size at 0 characters. Parameters: none Return Value: none void string ::clear () - Removes all characters (makes string empty) - Doesn't throw any error - Receives no parameters and returns nothing CPP // 1 min read std::to_string in C++ In C++, the std::to_string function is used to convert numerical values into the string. It is defined inside <string> header and provides a simple and convenient way to convert numbers of any type to strings.In this article, we will learn how to use std::to_string() in C++.Syntaxstd::to_strin 1 min read std::string::compare() in C++ The string::compare() function in C++ is used to compare a string or the part of string with another string or substring. It is the member function of std::string class defined inside <string> header file. In this article, we will learn how to use string::compare() in C++.The different ways to 4 min read std::basic_string::at in C++ Returns a reference to the character at the specified location pos. The function automatically checks whether pos is the valid position of a character in the string (i.e., whether pos is less than the string length), throwing an out_of_range exception if it is not. Syntax: reference at (size_type po 1 min read std::string::insert() in C++ In C++, the string insert() function is used to insert characters or a string at the given position of the string. For example,C++#include <bits/stdc++.h> using namespace std; int main() { string s = "Geeks"; // Inserting another string at the friend // of s s.insert(s.size(), "forGeeks"); cou 4 min read Vector data() in C++ STL In C++, the vector data() is a built-in function used to access the internal array used by the vector to store its elements. In this article, we will learn about vector data() in C++.Letâs take a look at an example that illustrates the vector data() method:C++#include <bits/stdc++.h> using nam 2 min read std::string::size() in C++ The std::string::size() function in C++ is a built-in method of the std::string class that is used to determine the number of characters in the string. All characters up to the null character are considered while calculating the size of the string.In this article, we will learn about string::size() 2 min read string at() in C++ The std::string::at() in C++ is a built-in function of std::string class that is used to extract the character from the given index of string. In this article, we will learn how to use string::at() in C++.Syntaxstr.at(idx)Parametersidx: Index at which we have to find the character.Return ValueReturn 1 min read std::string::push_back() in C++ The std::string::push_back() method in C++ is used to append a single character at the end of string. It is the member function of std::string class defined inside <string> header file. In this article, we will learn about std::string::push_back() method in C++.Example:C++// C++ Program to ill 2 min read Like