In this C++ tutorial, you will learn how to convert a std::string to a character array, copy string characters into a writable buffer, and use c_str() or data() when a C-style character pointer is required.
Convert C++ String to Char Array
A C++ std::string manages its own character storage, while a C-style string is represented by characters ending with a null character, '\0'. Therefore, the correct conversion depends on whether you need a separate writable character array or only a pointer to the characters already stored by the std::string.
If another function only needs to read the text, str.c_str() is usually enough and no copy is necessary. If you need an independent writable character buffer, allocate space for all characters plus the terminating null character and copy the string into that buffer.
| Requirement | C++ approach |
|---|---|
| Create a char array directly from a string literal | char charArr[] = "text"; |
| Read a std::string as a C-style string | str.c_str() |
| Access writable string storage in C++17 and later | str.data() |
| Create a separate writable buffer | Allocate str.size() + 1 characters and copy the text |
Copy with strcpy() | Destination must have room for the null terminator |
1. Create a Char Array from a String Literal
If the text is known when you write the program, you can initialize a character array directly from a string literal. The compiler automatically adds the terminating null character to the array.
This creates a character array directly; it does not convert an existing std::string object.
C++ Program
#include <iostream>
using namespace std;
int main() {
char charArr[] = "tutorialkart";
for(char ch: charArr)
cout << ch << " ";
}
Output
t u t o r i a l k a r t
The array actually has one additional element containing '\0'. A normal C-style output operation stops when it reaches that null terminator.
2. Copy Each std::string Character to a Char Array
You can copy the characters of a std::string one at a time when you need to process each character during the conversion.
In the following existing example, a while loop is used to copy the string characters.
C++ Program
#include <iostream>
using namespace std;
int main() {
string str = "tutorialkart";
char charArr[str.length()];
int i=0;
while (i < str.length()) {
charArr[i] = str[i];
i++;
}
for(char ch: charArr)
cout << ch << " ";
}
Output
t u t o r i a l k a r t
You may also use for loop with the same logic.
C++ Program
#include <iostream>
using namespace std;
int main() {
string str = "tutorialkart";
char charArr[str.length()];
for (int i=0; i < str.length(); i++) {
charArr[i] = str[i];
}
for(char ch: charArr)
cout << ch << " ";
}
Output
t u t o r i a l k a r t
Portability note: the two existing examples above use char charArr[str.length()]. A variable-length array is not part of standard C++. Some compilers accept it as an extension. Also, those arrays do not reserve an additional element for '\0', so they are not valid null-terminated C strings. If the resulting buffer must be used as a C-style string, allocate str.size() + 1 elements and write the null terminator explicitly.
3. Create a Writable Char Buffer from std::string Safely
When the string length is known only at runtime and you need a separate character array, a dynamically allocated array is a standard C++ solution. Reserve one extra character for the null terminator.
#include <iostream>
#include <memory>
#include <string>
int main() {
std::string str = "tutorialkart";
std::unique_ptr<char[]> charArr =
std::make_unique<char[]>(str.size() + 1);
for (std::size_t i = 0; i < str.size(); ++i) {
charArr[i] = str[i];
}
charArr[str.size()] = '\0';
std::cout << charArr.get();
}
Output
tutorialkart
The buffer contains a copy of the string. Changing charArr does not change str.
4. Use strcpy() and c_str() to Copy std::string to Char Array
c_str() returns a pointer to a null-terminated character sequence containing the text of the std::string. strcpy() can copy that sequence into a writable character array.
The destination must contain at least str.length() + 1 elements because strcpy() also copies the terminating null character. The standard header for strcpy() is <cstring>.
The following existing example demonstrates the basic strcpy() and c_str() approach.
C++ Program
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
string str = "tutorialkart";
char charArr[str.length()];
strcpy(charArr, str.c_str());
for(char ch: charArr)
cout << ch << " ";
}
Output
t u t o r i a l k a r t
Important: the destination in the existing example has only str.length() elements, while strcpy() needs one additional element for '\0'. Writing the terminator past the end of the array causes undefined behavior. The variable-length array is also not standard C++. Use a correctly sized buffer as shown below.
Safe strcpy() Example with Space for the Null Terminator
#include <cstring>
#include <iostream>
#include <memory>
#include <string>
int main() {
std::string str = "tutorialkart";
std::unique_ptr<char[]> charArr =
std::make_unique<char[]>(str.size() + 1);
std::strcpy(charArr.get(), str.c_str());
std::cout << charArr.get();
}
Output
tutorialkart
Use c_str() When You Do Not Need a Separate Char Array
Many C and C++ APIs accept a pointer to a null-terminated sequence of characters. If the function only reads the characters, you normally do not need to create another array. Use c_str() instead.
#include <iostream>
#include <string>
int main() {
std::string str = "tutorialkart";
const char* text = str.c_str();
std::cout << text;
}
Output
tutorialkart
The pointer returned by c_str() refers to storage managed by the std::string. Do not treat it as an independent array, and do not keep the pointer across operations that may reallocate or invalidate the string’s storage.
Use std::string::data() for Writable Character Access in C++17 and Later
In C++17 and later, calling data() on a non-const std::string returns a writable char*. This gives direct access to the string’s existing characters without creating a second array.
#include <iostream>
#include <string>
int main() {
std::string str = "cat";
char* chars = str.data();
chars[0] = 'C';
std::cout << str;
}
Output
Cat
You may modify existing character positions through this pointer, but you must not use the pointer to write beyond the string’s valid character range or attempt to resize the string by writing through the pointer.
Convert C++ String to vector<char> for a Resizable Character Buffer
If a resizable contiguous character buffer is acceptable, std::vector<char> avoids manual memory management. Add '\0' when the buffer must also behave as a C-style string.
#include <iostream>
#include <string>
#include <vector>
int main() {
std::string str = "tutorialkart";
std::vector<char> chars(str.begin(), str.end());
chars.push_back('\0');
std::cout << chars.data();
}
Output
tutorialkart
std::string and Char Array Are Different C++ Types
A std::string is not simply another name for a char array. std::string is a standard library class that manages storage, tracks its length, and provides operations such as concatenation, searching, insertion, and replacement. A built-in char[] is a fixed-size array of characters.
A C-style string is specifically a character sequence terminated by '\0'. A char array can exist without that terminator, in which case it is merely an array of characters and cannot safely be passed to functions that expect a null-terminated C string.
Null Terminator Size When Converting String to Char Array
If a string contains N visible characters, a separate C-style character array normally needs N + 1 elements:
String: H e l l o
Index: 0 1 2 3 4
Array: H e l l o \0
Size: 6 elements
Forgetting the extra element is a common source of buffer overflows when using functions such as strcpy().
Choosing a C++ String to Char Array Conversion
- Use
char arr[] = "text";when the text itself is a compile-time string literal. - Use
str.c_str()when an API needs read-only C-style text and you do not need a copy. - Use
str.data()in C++17 or later when you need writable access to the existing string characters without resizing the string. - Allocate
str.size() + 1characters when you need an independent null-terminated writable buffer. - If you use
strcpy(), include<cstring>and ensure the destination has enough space for the characters and the terminating'\0'. - Use
std::vector<char>when a resizable contiguous character buffer is more appropriate than a raw array.
C++ String to Char Array Summary
In this C++ Tutorial, we learned how to convert a string to char array, with the help of example C++ programs.
TutorialKart.com