C++ String Pointer

Introduction

A string has a member function .c_str() which returns a pointer to its first element.

It returns a pointer to a null-terminated character array for our string:

#include <iostream> 
#include <string> 

int main() // w ww.j a va2  s.  c  om
{ 
    std::string s = "Hello World."; 
    std::cout << s.c_str(); 
} 

This member function is of type const char* and is useful when we want to pass our std::string variable to a function accepting a const char* parameter.




PreviousNext

Related