-2

How to convert int and std::string to char* ?

I'm trying to do this :

int a = 1;
std::string b = "str";
char* x = a + b;

I don't want something like this:

std::stringstream ss;
std::string str2char;
ss << a;
str2char = ss.str() + "str";
std::vector<char> writable(str2char.size() + 1);
std::copy(str2char.begin(), str2char.end(), writable.begin());
x = &writable[0];

How to deal with this, please.

user3472134
  • 11
  • 1
  • 4

3 Answers3

3

One way would be to use a string stream (include <sstream>), output data of various types into it, and then grab the output as a string or as a const char*, like this:

std::stringstream ss;
int a = 1;
std::string b = "str";
ss << a << b;
std::string res(ss.str());
const char *x = res.c_str();

Demo on ideone.

If you need to convert to char*, not to const char*, make a copy of c_str instead - replace the last line as follows:

char *x = new char[res.size()+1];
strcpy(x, res.c_str());
// Use x here, then...
delete[] x;

Finally, you can use vector instead of a string to get a writable pointer without the need to delete. Note that this approach does not let you return your char* from a function, because its data would be tied to the scope of the vector with its characters. Here is a demo of this approach.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

If you are using C++11, std::to_string() is a good option:

int a = 1;
std::string b = "str";
std::string one = std::to_string(a);
std::string one_plus_b = (one + b);
const char * x = one_plus_b.c_str();

And as it was pointed out by other answers, 'x' can only be used inside the local scope.

glampert
  • 4,371
  • 2
  • 23
  • 49
  • 3
    The result of `(one+b)` will be a temp, so `c_str()` will point to deallocated memory. – Sergey Kalinichenko Mar 29 '14 at 02:23
  • 4
    No, it's not valid to use `x` at all (until it has been assigned to something else). – Benjamin Lindley Mar 29 '14 at 02:25
  • 1
    @glampert Errr, how does that make it valid? It's invalid exactly after `const char* x` is assigned – user1520427 Mar 29 '14 at 02:25
  • I'm trying to convert this to char* and not to const char* – user3472134 Mar 29 '14 at 02:29
  • `std::string one_b = std::to_string(a) + "str";`, then use `&one_b[0]` as your pointer. – M.M Mar 29 '14 at 02:31
  • Just tested the exact same code I posted in the answer with clang++ and 'x' was valid after the assignment. This makes sense to me, since the temp created by (one + b) can only be destroyed at the end of the scope. But I don't know if this is the standard behavior, to be honest... – glampert Mar 29 '14 at 02:34
  • 2
    @glampert "Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created. [12.2/3]" – user1520427 Mar 29 '14 at 02:37
  • Just because you happened to see the results you expected to see, does not mean `x` was valid. It's [**undefined behavior**](http://en.wikipedia.org/wiki/Undefined_behavior), so it is allowed to work as you expect, even if your expectations are wrong. – Benjamin Lindley Mar 29 '14 at 02:38
  • 1
    FYI - you can prolong the life of the temporary to the end of the scope like this: `const std::string& ref = std::to_string(a) + b;` - or just copy it. Re your clang test case, you'd probably actually see it failing if you used the stack for something (e.g. call another function) after the temporary went out of scope and before trying to use it.... – Tony Delroy Mar 29 '14 at 02:44
  • Yes @TonyD, very likely that the stack memory retained the temp string, even though the object was already destroyed. I wonder if static code analysis tools would normally catch this bug. Clang's highest warning level didn't. – glampert Mar 29 '14 at 02:51
  • It's definitely possible to pick up many simple mistakes using static analysis, but of course you can't expect a compiler to do that very comprehensively so many won't bother trying at all. Most compiler writers seem to be lagging a bit "just" implementing the mandatory C++11 features ;-). – Tony Delroy Mar 29 '14 at 02:56
0

int and std::string to char*?

Here are a couple one-liners:

char* p = strdup((std::to_string(a) + b).c_str());
...use p...
free(p);

...or, if your system provides it (I don't think it's in the C++ Standard itself)...

char* p = asprintf("%d%s", a, b.c_str());
...use p...
free(p);

Note that these are C library functions, and the memory allocation is malloc/realloc/free-family, which can not be mixed with new/delete (which means the pointer shouldn't be wrapped in a Standard smart pointer, though of course a free-specific or generic scope guard could be used).

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252