Error

Getting this error here:

warning: incompatible integer to pointer conversion
passing 'int' to parameter of type 'char *' [-Wint-conversion]
myitoa(n / 10, *s + 1);


Function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

void myitoa(int n, char *s)
{
	
    int i, sign;
  
    if ((sign = n) < 0)    /* record sign */
       n = -n;             /* make n positive */
	i = 0;
	
	if (n / 10)
		myitoa(n / 10, *s + 1);
	s[i++] = n % 10 + '0';
	
	if (sign < 0)
	  	s[i++] = '-';
  
}

Last edited on
Not enough info.
What do you mean?

Maybe it wants the reference & operator
Last edited on
He means that you haven't supplied enough info for us to solve the problem.
What is the signature of myitoa? If I had to guess, your dererencing a char pointer, thus passing a variable of type char, not char*.
To get rid of the compiler error:
1
2
// myitoa(n / 10, *s + 1); // line 12
myitoa( n / 10, s + 1 ); 


The logic for placing the sign is broken. And c-style strings need to be null-terminated.

Something like this, perhaps:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int myitoa( int n, char* s )
{
    int i = 0 ;

    if( n < 0 )
    {
         s[i++] = '-' ; /* record sign */
         ++s ;
         n = -n;  /* make n positive */
    }

	if( n/10 ) i += myitoa( n/10, s );

	s[i++] = n % 10 + '0';
	s[i] = 0 ; // null-terminate

	return i ; // return number of chars used
}
It works for me.

I guess it is purely recursive now?
Last edited on
Your original code was recursive; this too is recursive (it uses your original code as the modal)
Topic archived. No new replies allowed.