-6

ok so I am learning C and I try to use simple functions to understand basics and here I am stuck whith a segmentation fault I can't manage to make this code working h3lp please thanks you all !!!

#include <stdio.h>

#include <stdlib.h>

int     ft_sqrt(int nb) //square root
{
        unsigned int i;

        i = nb;

        while (nb < (i * i))
            i--;
        if (nb == (i * i))
            return (i);
        if (nb > (i * i))
            return (0);
}

void    ft_strcpy(char *d, char *s) // string copy
{
        while((*d++ == *s++))
            ;
}

int     ft_strlen(char *s) // string length
{
        int i = 0;
        while(s[i] != '\0')
            i++;
        return (i);
}

char    *ft_itoa(int n)  // integer to ascii
{
        char    *s;

        s = (char *)malloc(99);
        s += ft_strlen(s);
        *s = 0;
        while((*--s == n % 10 + '0') && (n /= 10))
            ;
        return (s);
}

int     ft_atoi(char *s)  //ascii to integer
{
        int i = 0;
        while(*s)
            i = 10 * i + *s++ - '0';
        return (i);
}

int     main()
{
        int     ft_sqrt(int nb);
        void    ft_strcpy(char *d, char *s);
        char    *ft_itoa(int n);
        int     ft_atoi(char *s);
        int     ft_strlen(char *s);

        int     a, *x;
        a = 0;
        char    c[40], d[4];
        c[40] = 0;
        d[4] = 0;

        a = ft_sqrt(1764);  //42 in a
        ft_strcpy(d, ft_itoa(a));  // a in d
        ft_strcpy(c, "The square root of 1764 is: ");
        x = ft_atoi(d);
        printf("\n\n\t%s%sand%cin ascii\n\n\n", c, d, x);



        return 0;
}

Just hack my code just wanna learn !!

CnewB
  • 1
  • 3
  • 7
    W0w!1! Y0u'r3 s0 l331 that y0u use numb3rs 1nst3ad of lett3rs?1? – Jashaszun Jul 28 '15 at 20:57
  • haha u see so ahead of it am beyond call me 1338 – CnewB Jul 28 '15 at 21:00
  • 2
    Hello and welcome to Stack Overflow! To attract good answers, you will need to edity our question to contain more information. What is your code currently doing? What is the desired behaviour? Please read [this](http://stackoverflow.com/help/how-to-ask). – Anders Jul 28 '15 at 21:06
  • `*x` --> `x`, and `ft_atoi` is wrong.(always 0, because 0*x=>0) – BLUEPIXY Jul 28 '15 at 21:07
  • thank you @Anders I will – CnewB Jul 28 '15 at 21:10
  • @BLUEPIXY could you be more specific with atoi ? – CnewB Jul 28 '15 at 21:17
  • 1
    `i *= N;` meant `i = i * N;` but `i = 0` So `i *= 10 + *s++ - '0';` should be `i = 10*i + *s++ - '0';` – BLUEPIXY Jul 28 '15 at 21:19
  • hey @Jashaszun don't u wanna fix this segmentation fault? – CnewB Jul 28 '15 at 21:35
  • 2
    @CnewB Not really, but thanks for the offer. – Jashaszun Jul 28 '15 at 21:36
  • @Jashaszun thank you anyway u too leet for that too easy for u u got no time for this – CnewB Jul 28 '15 at 21:41
  • @CnewB Nah, it's just that when a question looks like what yours did when I first opened it, I tend to downvote, vote to close, and then ignore. Your question is no different :) – Jashaszun Jul 28 '15 at 21:44
  • @Jashaszun ok i get it is my question better now ? – CnewB Jul 28 '15 at 21:46
  • 1
    in the `while` and `if` checks, make sure you use `==` instead of `=` to test for equality. I don't think this will solve the issue, but definitely worth trying. EX: not `if (nb = (i * i))` but `if (nb == (i * i))` etc... Also, to check if you're at the end of a string use `while(s[i] =! '\0') i++;` Not what you're using. That I think would solve the error. – Emad Y Jul 28 '15 at 21:48
  • thank you @user3469481 I've done what u've said but it didn't fix the seg fault – CnewB Jul 28 '15 at 22:13

2 Answers2

0

There are many bugs in your code

  1. When the nb is not a perfect square number, then it will give 0 always. Try it yourself. Use sqrt() instead (available under math.h header file)

  2. Your ft_strcpy() is not properly framed, use strcpy() under string.h header file instead. Prototype : void strcpy(char *str1, char *str2),here the content of str2 will be copied to the str1.

  3. char *ft_itoa(int n) may not properly work because you have provided wrong value to ft_strlen() which may turn out wrong value to be added to the pointer *s ( Why ?. Think your self , i'm not gonna tell you that elementary concept).

Do re-code your program and let me know if still can't fix the Error.

surajs1n
  • 1,493
  • 6
  • 23
  • 34
0

Hey @BLUEPIXY thanks for your help you fixed my atoi ! @psyco thank you here is my code fixed btw with the same value to ft_strlen() for *ft_itoa(int n).

#include <stdio.h>
#include <stdlib.h>

int     ft_sqrt(int nb)
{
        unsigned int i;

        i = nb;

        while (nb < (i * i))
            i--;
        if (nb = (i * i))
            return (i);
        if (nb > (i * i))
            return (0);
}    

void    ft_strcpy(char *d, char *s)
{
        while((*d++ = *s++))
            ;
} 

int     ft_strlen(char *s)
{
        int i = 0;
        while(s[i] != '\0')
            i++;
        return (i);
}

char    *ft_itoa(int n)
{
        char    *s;

        s = (char *)malloc(99);
        s += ft_strlen(s);
        *s = 0;
        while((*--s = n % 10 + '0') && (n /= 10))
            ;
        return (s);
}

int     ft_atoi(char *s)
{
        int i = 0;
        while(*s)
            i = 10 * i + *s++ - '0';
        return (i);
}

int     main()
{
        int     ft_sqrt(int nb);
        void    ft_strcpy(char *d, char *s);
        char    *ft_itoa(int n);
        int     ft_atoi(char *s);
        int     ft_strlen(char *s);

        int     a;
        a = 0;
        char    c[40], d[4];
        c[40] = 0;
        d[4] = 0;

        a = ft_sqrt(1764);
        ft_strcpy(d, ft_itoa(a));
        ft_strcpy(c, "The square root of 1764 is: ");
        printf("\n\n\t%s%s and %c in ascii\n\n\n", c, d, ft_atoi(d));

        return 0;
}

BugFixed thank you all

CnewB
  • 1
  • 3