Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a std::string to const char* or char*

How can I convert an std::string to a char* or a const char*?

like image 663
user37875 Avatar asked Dec 07 '08 19:12

user37875


People also ask

How do I convert a string to a char in C++?

The c_str() and strcpy() function in C++ C++ c_str() function along with C++ String strcpy() function can be used to convert a string to char array easily. The c_str() method represents the sequence of characters in an array of string followed by a null character ('\0'). It returns a null pointer to the string.

What is the difference between const char * and string?

string is an object meant to hold textual data (a string), and char* is a pointer to a block of memory that is meant to hold textual data (a string). A string "knows" its length, but a char* is just a pointer (to an array of characters) -- it has no length information.

Can a char * be passed as const * argument?

In general, you can pass a char * into something that expects a const char * without an explicit cast because that's a safe thing to do (give something modifiable to something that doesn't intend to modify it), but you can't pass a const char * into something expecting a char * (without an explicit cast) because that's ...

What is a const char * in C++?

char* const says that the pointer can point to a char and value of char pointed by this pointer can be changed. But we cannot change the value of pointer as it is now constant and it cannot point to another char.


1 Answers

If you just want to pass a std::string to a function that needs const char* you can use

std::string str; const char * c = str.c_str(); 

If you want to get a writable copy, like char *, you can do that with this:

std::string str; char * writable = new char[str.size() + 1]; std::copy(str.begin(), str.end(), writable); writable[str.size()] = '\0'; // don't forget the terminating 0  // don't forget to free the string after finished using it delete[] writable; 

Edit: Notice that the above is not exception safe. If anything between the new call and the delete call throws, you will leak memory, as nothing will call delete for you automatically. There are two immediate ways to solve this.

boost::scoped_array

boost::scoped_array will delete the memory for you upon going out of scope:

std::string str; boost::scoped_array<char> writable(new char[str.size() + 1]); std::copy(str.begin(), str.end(), writable.get()); writable[str.size()] = '\0'; // don't forget the terminating 0  // get the char* using writable.get()  // memory is automatically freed if the smart pointer goes  // out of scope 

std::vector

This is the standard way (does not require any external library). You use std::vector, which completely manages the memory for you.

std::string str; std::vector<char> writable(str.begin(), str.end()); writable.push_back('\0');  // get the char* using &writable[0] or &*writable.begin() 
like image 186
7 revs, 4 users 92% Avatar answered Sep 28 '22 06:09

7 revs, 4 users 92%