I'm writing a recursive function that takes a char array, which represents a number, and a pointer to a digit in that array. The point of the function is to increment the number just like the ++ operator. However, when I try it out on the number '819'. It doesn't increment it to '820' but instead changes it to '810' (it increments the last digit but doesn't do the recursion that I want). Can someone help me out with this? Thanks.
#include <stdio.h>
char* inc(char *num, char* p)
{
if( *p>='0' && *p<='8' )
{
*p++;
}
else if ( *p=='9' )
{
*p = '0';
inc(num, --p);
}
return num;
}
main()
{
char x[] = "819";
printf("%s\n", inc(x, x+strlen(x)-1) ); //pass the number and a pointer to the last digit
}