string data() in C++

data(): It returns const pointer to contents. This is a pointer to internal data. 

It is undefined to modify the contents through the returned pointer. 

To get a pointer that allows modifying the contents use &str[0] instead.

Parameter: No parameters are required for this function.

Syntax:

str.data()

For Example:

str = "abc"

str.data() = > It returns the pointer to content of string (i.e abc).

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{

    string str = "abc";

    cout << str.data() << "\n";

    return 0;
}


No comments:

Post a Comment