I'm trying to write an ITOA (integer to array) function using pointers.
So this is what I got so far. I debugged and it works just fine. The thing is, the printing itself doesn't work. I'm adding two screenshots.
Would appreciate some help.
int num_length(int number)
{
int count = 0;
while (number > 0)
{
count++;
number /= 10;
}
return count;
}
void itoa(int number, char *strptr)
{
int number_len = num_length(number);
char *start = strptr;
strptr += number_len - 1;
while (strptr >= start)
{
*strptr = number % 10;
number /= 10;
strptr--;
}
}
void print_string(char *strptr)
{
while (*strptr != '\0')
{
printf("%c", *strptr);
strptr++;
}
}
void main(void)
{
int number;
char number_in_string[N] = { '\0' };
char *strptr = &(number_in_string[0]);
printf("Enter a number: ");
scanf_s("%d", &number);
itoa(number, strptr);
print_string(number_in_string);
getch();
}