Why does itoa () change the passed char [] instead of returning char []?

Is there a reason the itoa () function returns void and stores the result in the char [] array passed to it instead of returning char []?

+4
source share
3 answers

The reason is because the function expects the caller to allocate and provide a buffer. This is very standard for C. If the function returned a new buffer, the caller would need to take care to free it, and this would cause the buffer to be dynamically allocated. By asking the caller to provide a buffer, the function allows you to allocate a buffer with automatic storage or dynamically allocate.

, itoa, , , , void.

+10

C .

, C ; .

  • , , . . , , .
  • static . , . , .
  • malloc() . , free() .

-, itoa() 1, , , .

, itoa(), ISO C, POSIX. -, C , , (Solaris Linux), . , , , sprintf snprintf.

+3

, itoa , void. , , const char * C. , itoa ANSI-C ( , ).

+2

All Articles