|
-
March 18th, 2009, 12:58 PM
#1
converting string to c-string
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.
-
March 18th, 2009, 01:17 PM
#2
Re: converting string to c-string
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.
-
March 18th, 2009, 03:17 PM
#3
Re: converting string to c-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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|