snippetcppCritical
How to convert a char array to a string?
Viewed 0 times
arrayhowconvertcharstring
Problem
Converting a C++
I have a char array like:
string to a char array is pretty straightorward using the c_str function of string and then doing strcpy. 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.Solution
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";
// or
str = arr;Code Snippets
char arr[ ] = "This is a test";
string str(arr);
// You can also assign directly to a string.
str = "This is another string";
// or
str = arr;Context
Stack Overflow Q#8960087, score: 430
Revisions (0)
No revisions yet.