I have the following working putchar() functions for integers:
void write_uint(unsigned n) {
if (n / 10) write_uint(n / 10);
putchar(n % 10 + '0');
}
void write_int(int n) {
if (n < 0) {
putchar('-');
write_uint(-(unsigned)n);
}
else write_uint(n);
}
I am trying to modify it to convert the integer to a char array using pointers instead of writing to console as follows:
void write_uint(unsigned n, char *p) {
if (n / 10) write_uint(n / 10, p);
*p++ = n % 10 + '0';
}
void write_int(int n, char *p) {
if (n < 0) {
*p++ = '-';
write_uint(-(unsigned)n, p);
}
else write_uint(n, p);
}
int n = 123456789;
char *str = malloc(13000), *p = str;
write_int(n, p);
*p++ = '\n';
Then print the string, but all I get is a bunch of empty lines.
Any help is appreciated