kingsmasher1 kingsmasher1 - 1 year ago 106
C++ Question

How to convert a char array to a string?

Converting a C++

to a char array is pretty straightorward using the
function of string and then doing
. However, how to do the opposite?

I have a char array like:
char arr[ ] = "This is a test";
to be converted back to:
string str = "This is a test

Answer Source

The string class has a constructor that takes a NULL-terminated C-string:

char arr[ ] = "This is a test";

string str(arr);

//  You can also assign directly to a string.
str = "This is another string";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download