在char*中附加一个int

use*_*875 21 c++ integer append char

你会如何char*在c ++中附加一个整数?

Jer*_*ten 25

首先将int转换为char*使用sprintf():

char integer_string[32];
int integer = 1234;

sprintf(integer_string, "%d", integer);
Run Code Online (Sandbox Code Playgroud)

然后将其附加到您的其他字符*,使用strcat():

char other_string[64] = "Integer: "; // make sure you allocate enough space to append the other string

strcat(other_string, integer_string); // other_string now contains "Integer: 1234"
Run Code Online (Sandbox Code Playgroud)


Syd*_*ius 10

你也可以使用stringstreams.

char *theString = "Some string";
int theInt = 5;
stringstream ss;
ss << theString << theInt;
Run Code Online (Sandbox Code Playgroud)

然后可以使用访问该字符串 ss.str();