I am trying to convert a stringstream
into a const char*
, but I always get a blank string.
My Code:
#include<string>
#include<sstream>
#include<stdio.h>
const char* test(std::string city, std::string street, int houseNumber, int postCode) {
std::stringstream ss;
ss << city << " " << street << " " << houseNumber << " " << postCode;
const std::string tmp = std::string{ss.str()};
const char* str = tmp.c_str();
return str;
}
int main(){
printf(test("demo", "Demo", 1, 1234));
}
Expected Output: demo Demo 1 1234
I tried all the enhancements mentioned in How to convert a std::string to const char* or char*?, but I had no success.