0
0
CppHow-ToBeginner · 2 min read

C++ How to Convert String to Char Array Easily

In C++, you can convert a std::string to a char array by using str.copy(charArray, length) or by accessing the string's internal buffer with stringVar.data() or stringVar.c_str().
📋

Examples

Inputhello
Outputchar array: ['h','e','l','l','o']
InputC++
Outputchar array: ['C','+','+']
Input
Outputchar array: []
🧠

How to Think About It

To convert a string to a char array, think of the string as a list of characters stored in a special object. You want to copy these characters into a plain array of chars. You can do this by copying each character one by one or by using built-in functions that give you access to the string's internal characters.
📐

Algorithm

1
Get the input string.
2
Create a char array with size equal to the string length plus one for the null terminator.
3
Copy each character from the string to the char array.
4
Add a null character '\0' at the end of the char array to mark its end.
5
Use or print the char array as needed.
💻

Code

cpp
#include <iostream>
#include <string>

int main() {
    std::string str = "hello";
    char charArray[6]; // length of str + 1 for '\0'
    str.copy(charArray, str.size());
    charArray[str.size()] = '\0'; // null terminate
    std::cout << "Char array: " << charArray << std::endl;
    return 0;
}
Output
Char array: hello
🔍

Dry Run

Let's trace converting the string "hello" to a char array.

1

Input string

str = "hello"

2

Create char array

charArray size = 6 (5 chars + 1 null)

3

Copy characters

charArray = ['h','e','l','l','o', '\0']

IndexcharArray[Index]
0h
1e
2l
3l
4o
5\0
💡

Why This Works

Step 1: Using std::string::copy

The copy method copies characters from the string to the char array but does not add a null terminator.

Step 2: Adding null terminator

You must manually add '\0' at the end of the char array to mark the end of the string for C-style strings.

Step 3: Printing char array

Printing the char array works like printing a C-string because of the null terminator.

🔄

Alternative Approaches

Using c_str() method
cpp
#include <iostream>
#include <string>

int main() {
    std::string str = "hello";
    const char* charArray = str.c_str();
    std::cout << "Char array: " << charArray << std::endl;
    return 0;
}
This returns a pointer to the internal string buffer; do not modify the returned array.
Using std::vector<char>
cpp
#include <iostream>
#include <string>
#include <vector>

int main() {
    std::string str = "hello";
    std::vector<char> charVec(str.begin(), str.end());
    charVec.push_back('\0');
    std::cout << "Char array: " << &charVec[0] << std::endl;
    return 0;
}
This method uses a dynamic array and is safer for variable sizes.

Complexity: O(n) time, O(n) space

Time Complexity

Copying each character from the string to the char array takes linear time proportional to the string length.

Space Complexity

The char array requires extra space equal to the string length plus one for the null terminator.

Which Approach is Fastest?

Using c_str() is fastest as it avoids copying, but the array should not be modified. Copying creates a separate modifiable array.

ApproachTimeSpaceBest For
std::string::copyO(n)O(n)Modifiable char array
std::string::c_str()O(1)O(1)Read-only access, no copy
std::vectorO(n)O(n)Dynamic size, safe modification
💡
Always add a null terminator '\0' when converting to a char array for C-style string compatibility.
⚠️
Forgetting to add the null terminator '\0' causes undefined behavior when printing or using the char array.