سؤال

وكيف إلحاق عدد صحيح إلى char* في ج ++؟

هل كانت مفيدة؟

المحلول

أولا تحويل الباحث إلى 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"

نصائح أخرى

ويمكنك أيضا استخدام stringstreams.

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

ويمكن بعد ذلك الوصول إلى سلسلة باستخدام ss.str();

وشيء من هذا القبيل:

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

هل يمكن تبسيط ليون باستخدام الحد الأقصى لطول عدد صحيح على النظام الخاص بك.

تحرير عفوا - لم ير "++". ومع ذلك، فإنه بديلا.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top