Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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_Arrays_String_Sorting_Insert - Fatal编程技术网

如何在C中插入字符串排序数组?

如何在C中插入字符串排序数组?,c,arrays,string,sorting,insert,C,Arrays,String,Sorting,Insert,如何使用插入排序算法对字符串数组进行排序?实际上我有3个字符串数组-->a,b,c,我想根据数组a对它们进行并行排序。我已经编写了我认为正确的代码,但给出了一个错误: int main() { /* the insertsort call is */ InsertSort (count, A, B, C); /* A, B, and C are arrays of strings */ /* count is the size of the array */ } void InsertSo

如何使用插入排序算法对字符串数组进行排序?实际上我有3个字符串数组-->
a
b
c
,我想根据数组a对它们进行并行排序。我已经编写了我认为正确的代码,但给出了一个错误:

int main() {

/* the insertsort call is */ InsertSort (count, A, B, C);
/* A, B, and C are arrays of strings */
/* count is the size of the array */

}

void InsertSort (int n, char *a[17], char *b[17], char *c[5]){
char *temp = malloc(17);
char *temp1 = malloc(17);
char *temp2 = malloc(5);
    int i, j;
    for (i=1; i<n; i++) {
        j=i-1;
        while (j>=0 && strcmp(a[j],a[j+1])<0) {
            strcpy(temp, a[j]);
            strcpy(a[j], a[j+1]);
            strcpy(a[j+1], temp);

            strcpy(temp1, b[j]);
            strcpy(b[j], b[j+1]);
            strcpy(b[j+1], temp1);

            strcpy(temp2, c[j]);
            strcpy(c[j], c[j+1]);
            strcpy(c[j+1], temp2);
            j--;
        }
    }
}
intmain(){
/*insertsort调用是*/insertsort(count,A,B,C);
/*A、B和C是字符串数组*/
/*count是数组的大小*/
}
void InsertSort(int n,char*a[17],char*b[17],char*c[5]){
char*temp=malloc(17);
char*temp1=malloc(17);
char*temp2=malloc(5);
int i,j;

对于(i=1;i=0&&strcmp(a[j],a[j+1]),请添加对
InsertSort
的调用,包括所传递参数的定义。您知道
strcpy
不分配任何内存?将未初始化的指针(
temp
)传递给它是UB。我建议阅读本文以更好地了解如何调试它。我认为..这篇文章不适合我的情况…无论如何,我仍然无法理解您正在尝试对三个
char*
数组进行排序,但其中只有一个(
a
)实际上用于键入排序,对吗?排序
A
时发生的交换应针对
B
C
中的相同索引进行,对吗?和fwiw,除了分别为A、B和C进行的交换之外,不需要动态分配。