好的,所以我正在学习 C,我尝试使用简单的函数来理解基础知识,在这里我遇到了分段错误,我无法使这段代码工作 h3lp,谢谢大家!
#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;
}
只是破解我的代码只是想学习!