How to convert std::string to const char* or char* in C++?
Hello everyone! I just started learning C++ and ran into a problem. I have a std::string object that I use to store text, but I need to pass it to a function that expects const char* or char*. For example, I'm working with a library that has a function:\n\nvoid printMessage(const char* msg);\n\nI'm trying to pass my string to it:\n\nstd::string myString = "Hello, world!";\nprintMessage(myString); // Compilation error!\n\nThe compiler gives an error that it cannot convert std::string to const char*. I understand that these are different types, but how do I correctly get a C-string from std::string? Also, I need to get char* (non-const) for some functions that modify the string. I tried using c_str(), but it returns const char*, and I need char*. Please tell me how to do this c
No answers yet.
Log in to leave an answer