Is it possible to pass the pointer of a std::string to a function which expects a **char? The function expects a **char in order to write a value to it.
Currently I am doing the following:
char *s1;
f(&s1);
std::string s2 = s1;
Is there no shorter way? It is obvious, that s2.c_str() does not work, since it returns const *char.
That’s the appropriate way to handle that sort of function. You cannot pass in the
std::stringdirectly because, while you can convert it to a C string, it is laid out in memory differently and so the called function would not know where to put its result.If possible, however, you should rewrite the function so it takes a
std::string&orstd::string *as an argument.(Also, make sure you
free()ordelete[]the C string if appropriate. See the documentation for whateverf()is to determine if you need to do so.)