Thread: itoa function trouble ??

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    91

    itoa function trouble ??

    Hi !

    This is a program that i wrote to convert an integer to a string. however I came up with a problem :

    The program does not work when I enter any negative number less than -999 and gives a wiered output

    Can anyone help me with this and tell me why ?

    I have placed ample comments in the program please let me know if still something is unclear.


    The code is :

    Code:
    #include<stdio.h>
    #include<string.h>
    
    void itoa(int n, char s[])
    {
         int i = 0;      /* variables to keep track of the index of the string */
         int j = 0;
         int k = 0;
         
         
         int sign = n;   /* variable to keep track of the sign of the integer */
         
         int temp;       /* temporary variable to store characters while reversing a string */
         
         /* make the number positive if it is negative */
         
         if (sign < 0)
            n = -n;
            
         /* run a loop and store each digit in the string ( in reverse order )*/
         
         do
         {
              s[i++] = n%10 + '0'; 
         }
         while((n/= 10) > 0);
         
         /* if the number was negative place a -ve sign at the end */
         
         if (sign < 0)
            s[i++] = '-';
         
         
         /* place a NULL character to mark the end of the string and bring the index marker i to the last character */
            
         s[i] = '\0';
         --i;
         
         /* loop to reverse the stirng */
         
        for( i = 0, j = (strlen(s) - 1); i < j; i++, j--)
         {
               temp = s[i];
               s[i] = s[j];
               s[j] = temp;
         }              
    }
    
    
    
    int main()
    {
        char s[] = "";
        int n = -1000;
        itoa(n, s);
        printf("%s\n", s);
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    sorry to bother but I found my mistake

    in the above program i just posted I made a mistake


    " Please delete the iteration --i in line 66 "

    don't need that since in the for loop I start decrementing j from (strlen(s) - 1)

    sorry !!!!

  3. #3
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Code:
     char s[] = "";
    You've still got a problem because you're not allocating enough memory for the 's' char variable. This will only allocate 1 byte for the '\0'

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Code:
     char s[]
    is an unsized array.
    When no one helps you out. Call google();

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > is an unsized array.
    No, it isn't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM