String Data Function - C++ Programming Tutorial
C++ Course / Strings / String Data Function

String Data Function

BLUF: Mastering String Data Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: String Data Function

C++ is renowned for its efficiency. Learn how String Data Function enables low-level control and high-performance computing in the tutorial below.

C++ String Data

This function duplicates the characters from the string into an array and provides the memory address of the resulting array created from the string conversion.

Syntax

Consider a string str along with a pointer p. The syntax would appear as:

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 instance showcases the string by utilizing the data method.

Example 2

Example

#include<iostream>
using namespace std;
int main()
{
	string source="1245";
	const char* target=source.data();
	cout<<target;
	return 0;
}

Output:

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience