gpt4 book ai didi

c - C 字符串转换中的段错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:49:36 25 4
gpt4 key购买 nike

下面是我的欧拉项目程序。尝试将字符串转换为数字时出现段错误;

main()
{
char me[] = "731671765313306249";
int counter = 0;
unsigned int product = 0;
unsigned int temp = 0;
char dup_me[5];
int j = 0;
printf("\n The String is:%s", me);

for(counter = 0; counter <= (strlen(me) - 5); counter ++)
{
temp = ((int)(me[counter]) * (int)(me[counter + 1]) * (int)(me[counter + 2]) * (int)(me[counter + 3]) * (int)(me[counter + 4]));
if (product < temp)
{
product = temp;
for(j = 0; j < 4; j ++)
{

dup_me[j] = me[counter + j];
printf("\nThis time: %d", atoi(dup_me[j]));
}
dup_me[j+1] = '\0';
}
}
printf("\n The products is ;%Ld", product);
printf("The producted numbers are:%s", dup_me);
return 0;
}

如果我评论这部分,它运行良好。

printf("\nThis time: %d", atoi(dup_me[j]));

我知道产品答案是错误的。它正在将字符转换为它们的 ascii 值。我需要有关此段错误的帮助。我需要将该 individula 字符(数字)转换为整数值

The codepad link

谢谢

最佳答案

我认为您的 me[] 可能无效。如果您想使用 me 作为缓冲区,请将其声明为 char* me。通过为它分配一个空字符串,您实际上只是创建了一个没有存储空间的字符串。

关于c - C 字符串转换中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20155477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com