2008-12-07 8 views

ответ

22

Сначала преобразовать Int в char* с помощью sprintf():

char integer_string[32]; 
int integer = 1234; 

sprintf(integer_string, "%d", integer); 

Затем, чтобы добавить его в другой символ *, используйте 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" 
+3

Это также будет работать в C. – Sydius 2008-12-07 02:46:24

9

Вы также можете использовать stringstreams.

char *theString = "Some string"; 
int theInt = 5; 
stringstream ss; 
ss << theString << theInt; 

Строка затем можно получить с помощью ss.str();

4

Что-то вроде:

width = floor(log10(num))+1; 
result = malloc(strlen(str)+len)); 
sprintf(result, "%s%*d", str, width, num); 

Вы могли бы упростить Len, используя максимальную длину для целого на вашей системе.

Редактировать oops - не видел "++". Тем не менее, это альтернатива.