Convert Array to String in C++
Turn a null-terminated char array into a std::string in C++ with assignment, the constructor, or assign(). Explains why the '\0' at the end is required here.
1#include <iostream>2#include <string>3using namespace std;45int main() {6 char arr[] = {'H', 'e', 'l', 'l', 'o', '\0'};78 // Method 1: Direct assignment9 string str1 = arr;1011 // Method 2: Using string constructor12 string str2(arr);1314 // Method 3: Using assign()15 string str3;16 str3.assign(arr);1718 cout << "Array: " << arr << endl;19 cout << "String (method 1): " << str1 << endl;20 cout << "String (method 2): " << str2 << endl;21 cout << "String (method 3): " << str3 << endl;2223 return 0;24}
Sample Output
String (method 1): Hello
Copied up to the terminator.
In this one
- How a char array with '\0' converts to a string
- Three equivalent ways to do it
- What goes wrong without the terminator
- How to convert an array with a known length but no terminator
How it works
- 1
arr holds H e l l o \0.
- 2
str1 = arr copies until '\0'.
- 3
str2(arr) does the same through the constructor.
- 4
str3.assign(arr) does the same through a method.
- 5
Print all four.
Edge cases
No terminator
Remove the '\0' from the initializer.
The conversion keeps reading past the array until it happens to hit a zero byte, which is undefined behavior. Use string(arr, 5) to give the length.
'\0' in the middle
Use {'H','i','\0','!','\0'}.
The conversion stops at the first '\0', so the string is just "Hi".
What's going on
The program converts a character array (like {'H', 'e', 'l', 'l', 'o', '\0'}) into a C++ string (like "Hello"). C++ strings are more convenient than character arrays because they automatically manage memory, provide length information, and offer many useful methods. This conversion allows you to use modern C++ string features with C-style data.
Example:
Header Files Used
Declaring Variables
The program declares:
char arr[] = {'H', 'e', 'l', 'l', 'o', '\0'};
Method 1: Direct Assignment
string str1 = arr;
This is the simplest and most common method:
How it works:
Advantages:
Example:
char arr[] = "Hello";
string s = arr; // s = "Hello"
Method 2: Using String Constructor
string str2(arr);
This method explicitly uses the string constructor:
How it works:
Advantages:
Example:
char arr[] = "Hello";
string s(arr); // Explicit construction
Method 3: Using assign()
string str3;
str3.assign(arr);
This method uses the assign() member function:
How it works:
Advantages:
Example:
string s = "Old";
char arr[] = "New";
s.assign(arr); // s now contains "New"
Other Methods (Mentioned but not shown in code)
Method 4: Using append()
string str4;
str4.append(arr);
Example:
string s = "Prefix";
char arr[] = "Suffix";
s.append(arr); // s = "PrefixSuffix"
Method 5: Using stringstream
#include <sstream>
stringstream ss;
ss << arr;
string str5 = ss.str();
Displaying Results
The program prints:
cout << "Array: " << arr << endl;
cout << "String (method 1): " << str1 << endl;
cout << "String (method 2): " << str2 << endl;
cout << "String (method 3): " << str3 << endl;
Output:
Array: Hello
String (method 1): Hello
String (method 2): Hello
String (method 3): Hello
All three methods produce the same string "Hello".
Important Considerations
Null Terminator:
Array vs Pointer:
Memory Management:
When to Use Each Method
Best Practice: Use direct assignment for simple conversions - it's clean and efficient.
Common Use Cases
Reading from C Functions:
Many C functions return char*:
char* result = someCFunction();
string s = result; // Convert to C++ string
Working with Legacy Code:
When interfacing with C libraries:
char buffer[100];
// ... fill buffer from C function
string data = buffer; // Convert to modern C++ string
Building Strings:
string s = "Start";
char addition[] = "End";
s.append(addition); // s = "StartEnd"
Safety Considerations
Null Terminator Required:
Example of Problem:
char arr[5] = {'H', 'e', 'l', 'l', 'o'}; // No null terminator!
string s = arr; // Undefined behavior - may read beyond array
Correct Way:
char arr[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; // With null terminator
string s = arr; // Safe
Or Use String Literal:
char arr[] = "Hello"; // Automatically includes null terminator
string s = arr; // Safe
Performance Considerations
Copying:
Memory:
return 0;
This ends the program successfully.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What length is string("Hi\0there")?
Pick an answer to check it.
Question 2
How do you convert char a[3] = {'a','b','c'} safely?
Pick an answer to check it.
Questions people ask
Does the string share memory with the array?
No. The string makes its own copy.
What if I have a vector<char>?
string s(v.begin(), v.end());
Is char arr[] = "Hello" the same as the brace list?
Yes, the string literal form adds the '\0' automatically.