C++ String Data
This function copies the characters of the string into an array. It returns the pointer to an array obtained from the conversion of string into array.
Syntax
Consider a string str and pointer p. Syntax would be:
Example
const char* p=str.data();
Parameter
This function does not contain any parameter.
Return value
It returns the pointer to an array.
Example 1
Example
#include<iostream>
using namespace std;
int main()
{
string str ="C++ Strings";
const char* p =str.data();
cout<<?String contains :?<<p;
return 0;
}
Output:
Output
String contains : C++ Strings
This example displays the string using data function.
Example 2
Example
#include<iostream>
using namespace std;
int main()
{
string source="1245";
const char* target=source.data();
cout<<target;
return 0;
}
Output: