VOOZH about

URL: https://www.geeksforgeeks.org/cpp/basic_string-c_str-function-in-c-stl/

⇱ basic_string c_str function in C++ STL - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

basic_string c_str function in C++ STL

Last Updated : 7 Feb, 2023

The basic_string::c_str() is a built-in function in C++ which returns a pointer to an array that contains a null-terminated sequence of characters representing the current value of the basic_string object. This array includes the same sequence of characters that make up the value of the basic_string object plus an additional terminating null-character at the end. 

Syntax:

const CharT* c_str() const

Parameter: The function does not accept any parameter. 

Return Value : The function returns a constant Null terminated pointer to the character array storage of the string. 

Below is the implementation of the above function: 

Program 1: 


Output
s1.size is equal to strlen(s1.c_str()) 
GeeksForGeeks 

Program 2: 


Output
The 1th character of string Aditya is A
The 2th character of string Aditya is d
The 3th character of string Aditya is i
The 4th character of string Aditya is t
The 5th character of string Aditya is y
The 6th character of string Aditya is a

Program 3:


Output
GeeksforGeeks
Comment
Article Tags:
Article Tags: