Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C字符串转换中的分段错误_C_String - Fatal编程技术网

C字符串转换中的分段错误

C字符串转换中的分段错误,c,string,C,String,下面是我的Euler项目计划。当我试图将字符串转换为数字时,出现了分段错误 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(coun

下面是我的Euler项目计划。当我试图将字符串转换为数字时,出现了分段错误

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;
}
我知道产品的答案是错误的。它正在将字符转换为ascii值。我需要帮助解决这个分割错误。我需要将那个单独的字符(数字)转换成整数值


谢谢

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

函数
atoi
的原型如下:
int-atoi(const-char*nptr)
所以它需要一个指向
char
的指针,而您给它一个
char
所以替换这一行:
printf(“\n这次:%d”,atoi(&dup\U me[j])
与此
printf(“\n这次:%d”,atoi(&dup\U me[j]))
一切正常

me
可能是问题的根源;但使用
me
是无效的,而不是
me
本身。数组足够大,可以容纳一个字节;这不足以容纳字符串中的任何其他数据。此外,此循环不执行
(计数器=0;计数器-5,它永远不会运行。@StevenMorad编辑了这个问题。这个链接应该告诉你问题不在我身上,而在atoi转换上。请取消对printf line的注释以运行它而不出现任何错误。啊,我明白了。你的意思是做
atoi(dup_me)
atoi接受一个字符串。当你做'atoi(dup_me[j]),您只是给它一个字符。
printf("\nThis time: %d", atoi(dup_me[j]));