Conversion from numeric type to const char*

What is the better way for convert a numeric type into a const char *? I use: (boost::lexical_caststd::string(myNumericVar)).c_str() It is a good idea? -- View this message in context: http://boost.2283326.n4.nabble.com/Conversion-from-numeric-type-to-const-cha... Sent from the Boost - Users mailing list archive at Nabble.com.

On Mon, Jan 30, 2012 at 8:17 AM, Claude
What is the better way for convert a numeric type into a const char *? I use:
(boost::lexical_caststd::string(myNumericVar)).c_str()
It is a good idea?
Using a *const char ** is prone to Undefined Behaviors. If there's a way to use *std::string *instead, then try to do that. But - if you *must *use a *const char ** [say for a 3rd party interface that requires it] then that's as good as any method. Be aware of the potential for an exception, and be ready to deal with it.

I would like to convert it into const char * why should use it with a C function... -- View this message in context: http://boost.2283326.n4.nabble.com/Conversion-from-numeric-type-to-const-cha... Sent from the Boost - Users mailing list archive at Nabble.com.

2012/1/30 Claude
What is the better way for convert a numeric type into a const char *? I use:
(boost::lexical_caststd::string(myNumericVar)).c_str()
It is a good idea?
Depends how you use the value. // Safe. puts(boost::lexical_caststd::string(myNumericVar).c_str()); const char* s = boost::lexical_caststd::string(myNumericVar).c_str(); // Don't do this! Here 's' is a dangling pointer; the string has already // been destroyed. puts(s); Roman Perepelitsa.

Ok. I could use strcpy() to copy the .c_str() result in my char array? -- View this message in context: http://boost.2283326.n4.nabble.com/Conversion-from-numeric-type-to-const-cha... Sent from the Boost - Users mailing list archive at Nabble.com.

2012/1/30 Claude
Ok. I could use strcpy() to copy the .c_str() result in my char array?
You could, although it's usually better to keep std::string and let it care about memory management. When you need to pass const char* to a C function (e.g., puts), use c_str(). Roman Perepelitsa.

Ok. I could use strcpy() to copy the .c_str() result in my char array? -- View this message in context: http://boost.2283326.n4.nabble.com/Conversion-from-numeric-type-to-const-cha... Sent from the Boost - Users mailing list archive at Nabble.com.

Claude wrote:
Ok. I could use strcpy() to copy the .c_str() result in my char array?
-- View this message in context: http://boost.2283326.n4.nabble.com/Conversion-from-numeric-type-to-const-cha... Sent from the Boost - Users mailing list archive at Nabble.com.
If you absolutely need to fill a char array use std::string's copy method for that: char buf[100]; boost::lexical_caststd::string(myNumericVar).copy(buf,100); See Josutti's The C++ Standard Library 11.2.4 Alternatively boost::iostreams::array_sink to directly stream to a char buffer. Jeff
participants (4)
-
Brian Allison
-
Claude
-
Jeff Flinn
-
Roman Perepelitsa