Hello,
beginners question, what is the easiest way to convert a string (string class) to a c-string? Since declaring a c-string demands you it's size when declared, I'm not sure what the best way to do this is. Thank you.
Printable View
Hello,
beginners question, what is the easiest way to convert a string (string class) to a c-string? Since declaring a c-string demands you it's size when declared, I'm not sure what the best way to do this is. Thank you.
If you want a const char* that points to the first char of a null terminated C-style string, use the c_str() member function of std::string.
The following code should make the point clearer:
#include <string>
using namespace std;
void main()
{
string str1("abc");
printf("%s\n", str1.c_str());
}
The above program should print abc