2

I am trying to convert an int to a cstring. I've decided to read the int into a regular string via stringstream, and then read the string into a char array. The following seems to be working, but I'm wondering if I'm just getting lucky with my compiler. Does the code seem sound? Thanks!

int zip = 1234;    

char zipString[30];

stringstream str;

str << zip;

str >> zipString;

cout << zipString;
4
  • 1
    better move this question to codereview.stackexchange.com Commented Jun 29, 2012 at 14:54
  • Any reason you need to have the string as a c-string rather than a std::string? The latter is generally a much better option unless you're hamstrung by legacy code. Commented Jun 29, 2012 at 14:58
  • I need to deal with each 'integer' in the string independently. It seemed like an array would allow me to pick them off in order the easiest. Commented Jun 29, 2012 at 15:01
  • 1
    @IniquiTrance: std::string can also be indexed like an array. Commented Jun 29, 2012 at 15:03

3 Answers 3

4

You can get a C++ std::string from the stream's str() function, and an immutable C-style zero-terminated string from the string's c_str() function:

std::string cpp_string = str.str();
char const * c_string = cpp_string.c_str();

You might be tempted to combine these into a single expression, str.str().c_str(), but that would be wrong; the C++ string will be destroyed before you can do anything with the pointer.

What you are doing will work, as long as you're sure that the buffer is large enough; but using the C++ string removes the danger of overflowing the buffer. In general, it's best to avoid C-style strings unless you need to use an API that requires them (or, in extreme circumstances, as an optimisation to avoid memory allocation). std::string is usually safer and easier to work with.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Out of curiosity, would you advise against doing what I did?
You're right! I thought stringstream.str() returned std::string&, not std::string :)
@IniquiTrance: In general I'd advise using std::string for strings unless there's a compelling reason to use something else.
2

Unless you have a specific reason that you need an array of char instead of a standard string, I'd use the latter. Although it's not strictly necessary in this case, I'd also normally use a Boost lexical_cast instead of explicitly moving things through a stringstream to do the conversion:

std::string zipString = lexical_cast<std::string>(zip);

Then, if you really need the result as a c-style string, you can use zipString.c_str() to get that (though it's still different in one way -- you can't modify what that returns).

In this specific case it doesn't gain you a lot, but consistent use for conversions on this general order adds up, and if you're going to do that, you might as well use it here too.

1 Comment

You gain fail fast behavior, which is a lot. I've never seen an entire organization check stream's fail bits consistently (@IniquiTrance included).
1

The std::string's c_str() member function returns a const char* (aka a C-style string).

std::string str = "world";
printf("hello, %s", str.c_str());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.