0

** Hello :)

So I have this task to write a program that will convert int to arg using malloc and well it all works fine, gdb shows no errors, but it is not printing any output in this form. If i delete itoa_buffer[i] = '\0'; than sometimes it shows output sometimes not. I do not know what is wrong here, seems fine.

I do not know where to ask for help as I like to follow my logic and find errors here instead of copying solutions from the internet. I willl appreciate some tips, its probably some small thing Id do not know and wont let me go further.**

#include<stdio.h>
#include "libft.h"

char    *ft_itoa(int n)
{
    int i;
    int z;
    char x;
    char *itoa_buffer;

    if (n == INT_MIN)
        return(ft_strdup("-2147483648"));
    if (n < 0)
    {
        n = -n;
        z = n;
        i = 1;
    }
    else
    {
        z = n;  
        i = 0;
    }
    while(z > 0)
    {
        z = z/10;
        i++;
    }   
    if(!(itoa_buffer = (char *)malloc((i+1)*sizeof(char))))
        return(0);
    i = i + 1;
    while(--i)
    {   
        x = (n%10 + '0');
        itoa_buffer[i] = x;
        n = n/10;
        if(n == 0 && i == 2)
        {
            i--;
            itoa_buffer[i] = '-';
            i--;
            break;
        }
    }
    itoa_buffer[i] = '\0'; // it stopped showing answers when i tried to add this symbol at the end.
    return(itoa_buffer);
}

int main()
{
    int n;
    
    n = 1980;
    printf("%s", ft_itoa(n));
}
Tony
  • 25
  • 1
  • 6
  • You should simplify this a lot. For example you know that 11 characters + 1 null term is max, so simply malloc that much memory. The only purpose of malloc in your code is to preserve data across scopes, not "save some bytes of memory if the number is shorter". Then you can simply add the special case sign character from the start and from there do the rest of the conversion regardless of sign. – Lundin Feb 23 '21 at 11:43
  • Hmm, I see what you mean. This would be way easier if I could do it this way any time. But i think unfortunatelly they (my school) teach us to do malloc only as much as we need and later on it would not pass computer tests. Good point though, thank you! – Tony Feb 23 '21 at 13:39
  • That's obsolete thinking from back in the days where you needed ten mammoth tusks in order to produce 1kb of RAM. There are no mammoths any longer though, and we got this fancy new computer called 286 - we're talking megabytes here! So we no longer need to care about if we allocate 12 bytes or 5 bytes. – Lundin Feb 23 '21 at 15:27

1 Answers1

1

You are putting '\0' as the first character of the string.

Instead of that, you should put that as the last character.

Instead of this part

    i = i + 1;
    while(--i)
    {   
        x = (n%10 + '0');
        itoa_buffer[i] = x;
        n = n/10;
        if(n == 0 && i == 2)
        {
            i--;
            itoa_buffer[i] = '-';
            i--;
            break;
        }
    }
    itoa_buffer[i] = '\0';

you should do this

    itoa_buffer[i] = '\0';
    while(i--)
    {   
        x = (n%10 + '0');
        itoa_buffer[i] = x;
        n = n/10;
        if(n == 0 && i == 1)
        {
            i--;
            itoa_buffer[i] = '-';
            break;
        }
    }
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Why delete his second ```i--```? – steph Feb 23 '21 at 10:48
  • Because it is meaningless (the result is not used) in my version. – MikeCAT Feb 23 '21 at 10:48
  • Maaann!!! Since yesterday, even today i was writing it on paper in front of me writing 0 as '\0' and i did not notice the error! Thank you!! I am so glad that my logic after all was good! – Tony Feb 23 '21 at 10:50
  • Why not then also delete the first ```i--``` and substitute ```itoa_buffer[--i] = '-';```? – steph Feb 23 '21 at 10:52
  • @Steph we needed 2nd i-- only to diminuish "i" value to write the next caracter that was '\0' after the loop. With this solution '-' is written on the last position of the array, there is nothing more left :). I hope it helps a bit also, I am still learning, trying my best with explanation :) – Tony Feb 23 '21 at 10:53
  • Then `itoa_buffer[0] = '-';` should be better because the condition `i == 1` exists. – MikeCAT Feb 23 '21 at 10:55