In this C++ tutorial, you will learn how to convert a character array to std::string. The correct method depends mainly on whether the character array is null-terminated and whether you already know its length.
Convert a Char Array to std::string in C++
A C-style character array and a C++ std::string are different types. A character array may either contain a null character ('\0') marking the end of text, or it may simply contain a fixed number of characters. That distinction determines which std::string constructor or function is safe to use.
For a null-terminated array, you can construct or assign a std::string directly from the array. For an array that is not null-terminated, pass the number of characters explicitly. You can also append characters one at a time when your program needs to process each element.
In standalone C++ code that uses std::string, include the standard <string> header. The additional examples below do this explicitly.
| Char array form | Recommended conversion | Why |
|---|---|---|
| Null-terminated text | std::string str(charArr); | Stops at the first '\0' |
| Known length, no terminator required | std::string str(charArr, length); | Copies exactly length characters |
| Need to process each character | Range-based for loop | Lets you inspect or transform characters before appending |
Single char | std::string str(1, ch); | Creates a string containing one character |
Method 1: Construct std::string from a List of Characters
If the characters are written directly in the program, std::string can be initialized from a list of char values. This example does not convert a separate char[] variable; instead, the initializer list is used to construct the string.
C++ Program
#include <iostream>
using namespace std;
int main() {
string str = {'t','u','t','o','r','i','a','l','k','a','r','t'};
cout << str;
}
Output
tutorialkart
Method 2: Append a Null-Terminated Char Array with string.append()
When the character array is null-terminated, you can create an empty string and pass the array to string::append(). The function reads characters until it reaches the terminating null character.
C++ Program
#include <iostream>
using namespace std;
int main() {
string str;
char charArr[] = {'t','u','t','o','r','i','a','l','k','a','r','t','\0'};
str.append(charArr);
cout << str;
}
Please note that the last character in our character array is a null character.
Output
tutorialkart
append() adds characters to the end of an existing string. If the array is not null-terminated, use an overload that also receives the character count instead of calling append(charArr) alone.
Method 3: Build a String from a Char Array with a Range-Based For Loop
A range-based for loop is useful when you want to visit every element of a fixed-size character array and append each character to a string.
In the following example, each character in the array is appended to the string with str += ch.
C++ Program
#include <iostream>
using namespace std;
int main() {
string str = "";
char charArr[] = {'t','u','t','o','r','i','a','l','k','a','r','t'};
for(char ch: charArr)
str += ch;
cout << str;
}
Output
tutorialkart
You may also use for loop with the same logic.
Method 4: Use the std::string Constructor with a Char Array
The std::string(const char*) constructor expects the supplied character sequence to be null-terminated. A valid C-style string therefore needs a '\0' after its last visible character.
Important: the following existing example shows the one-argument constructor syntax, but its array does not contain a terminating '\0'. Passing that array to the constructor can read past the end of the array, so the program has undefined behavior. Use the safe constructor forms shown after it.
C++ Program
#include <iostream>
using namespace std;
int main() {
char charArr[] = {'t','u','t','o','r','i','a','l','k','a','r','t'};
string str(charArr);
cout << str;
}
Possible output from this unsafe program; not guaranteed
tutorialkart
Safe Constructor Conversion for a Null-Terminated Char Array
The simplest safe form is to define the array from a string literal. C++ automatically includes the terminating null character in the array.
#include <iostream>
#include <string>
int main() {
char charArr[] = "tutorialkart";
std::string str(charArr);
std::cout << str;
}
Output
tutorialkart
Convert a Char Array to String When the Length Is Known
A character array does not have to end with '\0' if you pass its length to the std::string constructor. This is the preferred form for fixed-size character buffers whose length is already known.
#include <iostream>
#include <string>
int main() {
char charArr[] = {'C', '+', '+', '2', '0'};
std::string str(charArr, sizeof(charArr));
std::cout << str;
}
Output
C++20
Here, sizeof(charArr) gives the number of elements because each element is a char. This works while charArr is an actual array in the same scope. After an array is passed to a function as a pointer, sizeof(pointer) does not give the original array length.
Append a Non-Null-Terminated Char Array by Length
If you already have a destination string, use the length-taking overload of append(). It copies exactly the requested number of characters and does not search for a null terminator.
#include <iostream>
#include <string>
int main() {
char charArr[] = {'d', 'a', 't', 'a'};
std::string str = "raw-";
str.append(charArr, sizeof(charArr));
std::cout << str;
}
Output
raw-data
Convert a Single char to std::string
A single char is not a character array. To create a one-character string, use the std::string constructor that accepts a count and a character.
#include <iostream>
#include <string>
int main() {
char ch = 'A';
std::string str(1, ch);
std::cout << str;
}
Output
A
std::to_string() is not the function for this conversion. It converts numeric values such as integers and floating-point values to their textual representation. Using a char with std::to_string() promotes it to an integer and produces the character’s numeric value rather than a one-character string.
Convert an unsigned char Array to std::string Only When It Contains Text Bytes
An unsigned char array is often used for raw bytes rather than text. If the bytes really represent text in a known encoding, you can copy them into a std::string using an explicit byte count. Do not assume arbitrary binary data is valid readable text.
#include <iostream>
#include <string>
int main() {
unsigned char bytes[] = {'H', 'e', 'l', 'l', 'o'};
std::string str(
reinterpret_cast<const char*>(bytes),
sizeof(bytes)
);
std::cout << str;
}
Output
Hello
Null Terminators and Length Matter in Char Array to String Conversion
- Use
std::string str(charArr);only whencharArris null-terminated. - Use
std::string str(charArr, length);when the array length is known or the array is not null-terminated. - Use
str.append(charArr, length);when adding a fixed number of characters to an existing string. - Use a loop when each character needs validation, filtering, or transformation before it is added.
- Do not use
std::to_string()to convert a char array or a single character into text.
Choosing the Right C++ Char Array to String Method
For ordinary C-style text, a null-terminated array can be passed directly to the std::string constructor. For fixed buffers and non-null-terminated arrays, pass the character count explicitly so that the conversion cannot read beyond the intended range. In this C++ Tutorial, we covered constructor-based conversion, append(), range-based loops, known-length arrays, single characters, and text stored in unsigned char arrays.
TutorialKart.com