Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How to convert a string to a char *?

char convert String
0
Posted

How to convert a string to a char *?

0

This question deals with Standard C++’s string class. Please note that this is wrong: #include int main() { std::string ComeauCpp(“Check out Comeau C++”); char *p = ComeauCpp; // nope } To wit, there is no conversion from string to char *. Some folks would try to “correct” this by doing this: char *p = &ComeauCpp; // If at first you don’t succeed, try, try, again But this is wrong too, because there is no conversion from string * to char *. As if two wrongs were not enough, some would still try to correct this as: char *p = (char *)&ComeauCpp; // If two wrong’s don’t make a right, try three under that premise that there was now a char * on either side. The problem here is that this assumes that the string class is implemented with a char array buffer, which may not be true. And even if it were, it assumes that the buffer is at the start of the string class, not to mention a violation of encapsulation. The right way to do this is to use strings conversion feature, its member fu

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.