【发布时间】:2021-01-17 09:01:49
【问题描述】:
当我尝试运行这个程序时,它不起作用。当我去调试时,弹出一条消息“程序收到信号sigsegv,分段错误”
我用 gdb 调试程序,这是消息。 程序收到信号 SIGSEGV,分段错误。 0x0000555555555195 in ft_atoi (str=0x555555556004 "111") at ft_atoi.c:33 33 while (ft_isspace(str[i]))
#include <string.h>
#include <stdio.h>
static int ft_isspace(char c)
{
if (c == '\n' || c == '\f' || c == ' ' ||
c == '\r' || c == '\v' || c == '\t');
return (1);
return (0);
}
int ft_atoi(const char *str)
{
int i;
int n;
int signe;
n = 0;
i = 0;
signe = 1;
while (ft_isspace(str[i]))
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i++] == '-')
signe = -1;
}
while (str[i] >= '0' && (str[i] <= '9') && str[i] != '\0')
{
if (((unsigned long)n > 9223372036854775807 / 10) ||
(((unsigned long)n == 9223372036854775807 / 10) &&
((unsigned long)n % 10) > 7))
return ((signe > 0 ? -1 : 0));
n = n * 10 + (*(str + (i++)) - '0');
}
return (n * signe);
}
int main()
{
printf("%d", ft_atoi("111"));
return 0;
}
【问题讨论】:
-
);就是这样。if(....);;。;60 年代以来一直在玩捉迷藏。 -
@KamilCuk 不是因为 clang 格式 ????
-
@KamilCuk 我没明白你的意思,请解释一下
-
启用编译器警告
标签: c