Std::String to Char*

std::string to char*

It won't automatically convert (thank god). You'll have to use the method c_str() to get the C string version.

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

Note that it returns a const char *; you aren't allowed to change the C-style string returned by c_str(). If you want to process it you'll have to copy it first:

std::string str = "string";
char *cstr = new char[str.length() + 1];
strcpy(cstr, str.c_str());
// do stuff
delete [] cstr;

Or in modern C++:

std::vector<char> cstr(str.c_str(), str.c_str() + str.size() + 1);

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

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()

Conversion of std:string to char*

When I run the code, an error appears when I call this function:

candidate function not viable: no known conversion from 'std::string' (aka 'basic_string') to 'char *' for 1st argument

You did not show the actual code that is trying to pass the std::string to the receiver() function, but the error message is very clear. You simply cannot assign a std::string directly to a non-const char*, which is exactly what the compiler is complaining about.

However, you can get a const char* from a std::string using its c_str() method, and then you can const_cast that to a char* (as long as the function does not try to modify the char data), eg:

std::string result = ...;
receiver(const_cast<char*>(result.c_str()), static_cast<int>(result.size()));

Or, you can simply use &result[0] instead (which is guaranteed in C++11 and later to be contiguous and null-terminated), or you can use result.data() in C++17 and later, eg:

std::string result = ...;
receiver(&result[0]/*result.data()*/, static_cast<int>(result.size()));

Or, you could simply change result from std::string to AnsiString, as its c_str() method returns a non-const char*, eg:

AnsiString result = AnsiString::StringOfChar('0', 8) + myDataModule->getId();
receiver(result.c_str(), result.Length());

Either way, if receiver() only needs to read from data and not modify its content, then it should be changed to take data as a const char* instead (especially since that is what strcpy() expects anyway), eg:

void receiver(const char * data, int num)

Then you can use result.c_str() as-is, no trickery, whether result is an AnsiString or a std::string.

Converting from std::string to char * in C++

Use

std::string bla("bla");
char* blaptr = &bla[0];

This is guaranteed to work since C++11, and effectively worked on all popular implementations before C++11.

If you need just a const char*, you can use either std::string::c_str() or std::string::data()

Preferred conversion from char (not char*) to std::string

std::string has a constructor that takes a number and a character. The character will repeat for the given number of times. Thus, you should use:

std::string str(1, ch);

C++ copy std::string to char array with no null termination

Use memcpy instead of strcpy, that's what it's there for:

memcpy(blocks[0].data, buffer.data(), sizeof(blocks[0].data)); 


Related Topics



Leave a reply



Submit