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代码中使用strcpy对字符串数组排序时出现分段错误_C_String_Sorting_Crash_Strcpy - Fatal编程技术网

在c代码中使用strcpy对字符串数组排序时出现分段错误

在c代码中使用strcpy对字符串数组排序时出现分段错误,c,string,sorting,crash,strcpy,C,String,Sorting,Crash,Strcpy,我正在尝试对字符串数组进行排序。如果我删除该行 strcpy(a[j-1],a[j]); 终端没有崩溃 字符串数组以这种方式分配,以防出现问题 array=(char **)malloc(sizeof(char *)*N); for(i=0;i<N;i++) array[i]=(char *)malloc(sizeof(char)*6); array=(char**)malloc(sizeof(char*)*N); 对于(i=0;i0) { strcpy(温度

我正在尝试对字符串数组进行排序。如果我删除该行

strcpy(a[j-1],a[j]);
终端没有崩溃

字符串数组以这种方式分配,以防出现问题

array=(char **)malloc(sizeof(char *)*N);

    for(i=0;i<N;i++)
        array[i]=(char *)malloc(sizeof(char)*6);
array=(char**)malloc(sizeof(char*)*N);
对于(i=0;i0)
{
strcpy(温度,a[j-1]);
strcpy(a[j-1],a[j]);
strcpy(a[j],温度);
}
}
}

为什么我会崩溃?

请确保在处理字符串时为空终止符允许额外的字节。

是否确保所有字符串都没有长度
>5
?如果字符串有6个字符,如“dancer”,则需要7个字节,因为需要一个字节来保存空值(0)terminator.似乎就是这样……感谢您的数据布局,您不需要使用
strcpy
复制字符串的内容;交换指向char的指针就足够了。如何将数据读入
a
元素所引用的内容?
void bubblesort1(char **a,int K)
{
    int i,j;
    char temp[6];

    for(i=1; i<K; i++)
        for(j=(K-1); j>=i; j--)
        {
            if(strcmp(a[j],a[j-1])>0)
            {
                strcpy(temp,a[j-1]);
                strcpy(a[j-1],a[j]);
                strcpy(a[j],temp);

            }

        }

}