Home > Back-end > Small white asked a questions about the long integer is converted into a string
Small white asked a questions about the long integer is converted into a string
Time:12-17
The final * STR='\ 0'; How is add terminator at the end of the string, * STR is not a pointer to the first string array? How here again become the last one? Please help have a look at the great god, thank you
CodePudding user response:
The original poster can study * str++, STR (*) + +, * (str++)
CodePudding user response:
Because the previous operation str++; This operation will make the position of the pointer move back one element; Long plastic string, also can consider to use sprintf, snprintf function, more simple,
sprintf (buf, "% lu", dat);
CodePudding user response:
If you are your own practice, for reference only, otherwise is to use system apis that ltoa
void Long2Str (long _l, char * outStr) { Char cTemp [20]={0};//an array of temporarily is the reverse of the string Char * pTemp=cTemp; If (_l & lt; 0) { * outStr++='-'; _l *=1; } Long l1. Long l2=_l; Do { L1=l2%10; * pTemp++=l1 + '0'. L2/=10; } while (l2 & gt; 0); PTemp -;
//will reverse the string of positive sequence copied to the output string While (pTemp & gt;=cTemp) { * outStr++ pTemp=* -; } }
Int _tmain (int arg c, _TCHAR * argv []) { Long l=1125412; Char STR [20]={0}; Long2Str (l, STR); Printf (" % s \ n ", STR); }
CodePudding user response:
The reason on this line:
* str++=buf [I] + '0'. //each loop first take STR value: * STR=buf [I] + '0' //then STR value add 1: str++ //while loop at the end of the STR pointer to the end of the string,