C++ Concatenating const char * with string, only const char * prints -


i trying cout const char *

this how convert int string , concatenate const char*

char temptextresult[100]; const char * tempscore = std::to_string(6).c_str(); const char * temptext = "score: "; strcpy(temptextresult, temptext); strcat(temptextresult, tempscore); std::cout << temptextresult; 

the result when printing is: score:

does know why 6 not printing?

thanks in advance.

as docs c_str say, "the pointer returned may invalidated further calls other member functions modify object." includes destructor.

const char * tempscore = std::to_string(6).c_str(); 

this makes tempscore point temporary string no longer exists. should this:

std::string tempscore = std::to_string(6); ... strcat(temptextresult, tempscore.c_str()); 

here, you're calling c_str on string continues exist.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -