Getting to know the main characters: std::string and an old friend char*
\n\nImagine a situation: you are writing code in C++, and suddenly you need to pass text to an old C library that requires const char*. Or you want to use a function that modifies a string "in place" via char*. And you have a modern, convenient std::string. A legitimate question arises: "How to convert a std::string to const char* or char*?".
This question is one of the most popular on Stack Overflow among beginner C++ developers. And for good reason: misunderstanding how strings and pointers work can lead to memory leaks, program crashes, and strange bugs. But don't be afraid! We'll break it all down now.
\n\nIn this article you will learn:
\n- \n
- How to safely get
const char*fromstd::string. \n - How to get a modifiable
char*and when it is needed. \n - What pitfalls await a beginner on this path. \n
- How to avoid typical mistakes when working with C++ string conversion. \n
The c_str() method: your main helper
\n\nThe simplest and safest way to get const char* is to call the c_str() method on a std::string object. Why is it called that? Because it returns a C-style string (null-terminated string).
#include <iostream>\n#include <string>\n\nint main() {\n std::string message = "Hello, C++ world!";\n const char* c_message = message.c_str();\n \n std::cout << c_message << std::endl;\n return 0;\n}\n\nThe c_str() method guarantees that the returned character array ends with a null character ('\\0'). This is critically important for all C functions that work with strings (e.g., strlen, fopen, printf).
Important warning: string lifetime
\n\nThe pointer obtained via c_str() points to the internal buffer of std::string. As soon as the original string is modified or destroyed, this pointer becomes invalid. Never save it "for later"!
std::string* get_string() {\n return new std::string("Temporary data");\n}\n\nint main() {\n const char* bad_ptr = get_string()->c_str();\n delete get_string(); // bad_ptr is now dangling!\n // Using bad_ptr is no longer allowed — this is undefined behavior\n}\n\nThe data() method: almost the same, but there is a nuance
\n\nBefore C++11, the data() method returned const char*, but without a guarantee of null-termination. Starting from the C++11 standard, data() and c_str() work identically — both return a null-terminated string. However, for backward compatibility and code clarity, it is recommended to use c_str() when you specifically need a C-string.
std::string text = "C++11 and newer";\nconst char* ptr1 = text.c_str(); // Explicit: need a C-string\nconst char* ptr2 = text.data(); // Also works, but less obvious\n\nIf you are writing code that must compile on older standards (pre-C++11), use only c_str() for safe string conversion.
How to get a non-const, regular char*?
\n\nSometimes a modifiable char* pointer is required. For example, you are calling a C function that modifies a string: strtok(), sprintf(), or some old file handling library. There is no direct way to "extract" char* from std::string — and this is done intentionally to protect you from errors.
But there is a workaround: copy the contents of the string into a new array.
\n\n#include <iostream>\n#include <str