Andrei Titaは、NULLターミネータで発生した問題をすでに示しています。別の方法を紹介しますので、さまざまなアプローチを比較対照できます。
int intToStr(unsigned int num, char *s)
{
// We use this index to keep track of where, in the buffer, we
// need to output the current character. By default, we write
// at the first character.
int idx = 0;
// If the number we're printing is larger than 10 we recurse
// and use the returned index when we continue.
if(num > 9)
idx = intToStr(num / 10, s);
// Write our digit at the right position, and increment the
// position by one.
s[idx++] = '0' + (num %10);
// Write a terminating NULL character at the current position
// to ensure the string is always NULL-terminated.
s[idx] = 0;
// And return the current position in the string to whomever
// called us.
return idx;
}
私の代替案は、バッファに出力する文字列の最終的な長さも返すことに気付くでしょう。
今後のコースワークで頑張ってください!