I am trying to create a char array in C, to fill it with the digits of an int, but the int can be of any number of digits.
I\'m using a created function called
For my money, there is one solution which has gone unmentioned but which is actually simpler than any of the above. There is a combined allocating version of sprintf called "asprintf" available on Linux and most BSD variants. It determines the necessary size, mallocs the memory, and returns the filled string into the first argument.
char * a;
asprintf(&a, "%d", 132);
// use a
free(a);
Using a stack allocated array certainly removes the need for the free, but this completely obviates the need to ever separately calculate the size.