Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.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
strcpy给出了分段错误_C_Recursion_Strcpy - Fatal编程技术网

strcpy给出了分段错误

strcpy给出了分段错误,c,recursion,strcpy,C,Recursion,Strcpy,下面是一个递归函数,用于将整数转换为字符串 char* int_to_word(int word_int){ static char new[2]; char alphabet[26]={"abcdefghijklmnopqrstuvwxyz"}; new[0]=alphabet[word_int%27-1]; //new[1]='\0'; if(word_int/27==0){ return new; } static c

下面是一个递归函数,用于将整数转换为字符串

char* int_to_word(int word_int){
    static char new[2];
    char alphabet[26]={"abcdefghijklmnopqrstuvwxyz"};
    new[0]=alphabet[word_int%27-1];
    //new[1]='\0';
    if(word_int/27==0){
        return new;
    }
    static char *word;
    strcpy(word,strcat(int_to_word(word_int/27),new));
    return word;
}

我在strcpy(word,strcat(int_to_word(word_int/27),new)行中遇到了一个分段错误
word\u int
>26时。据我所知,没有理由不起作用。我最好的猜测是,在复制到
word
之前,我不知何故需要分配
word
,但将初始值设定项更改为
static char*word=(*char)malloc(100)
没有帮助。

word指向什么内存?您从未初始化
word
,您需要使用
malloc()
为其分配内存。将静态变量与递归函数一起使用是行不通的。您将尝试在相同的字符串之间进行复制。您似乎试图通过使用静态变量来绕过无法返回本地数组的事实。但这会导致一个新问题:每次调用函数时,都会覆盖从不同调用返回的数组不是你想要的,是一个你可能想要的
char foo[]=“bar”