简体   繁体   English

用qsort排序字符串数组

[英]Sorting array of strings with qsort

This program takes an integer value which then determines the amount of strings that can be inputted, once the user has entered the amount of strings that they specified they may input another integer and then input those amount of strings. 该程序采用一个整数值,该整数值然后确定可以输入的字符串数量,一旦用户输入了他们指定的字符串数量,他们可以输入另一个整数,然后输入这些字符串数量。 Once done the program sorts the strings based on their lengths in descending order. 完成后,程序将根据字符串的长度以降序对它们进行排序。 However the qsort doesn't work, it ends up outputting how the order of when the strings were originally entered. 但是qsort不起作用,最终输出最初输入字符串的顺序。

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>


int sort(const void * a, const void * b){
    size_t fa = strlen((const char *)a);
    size_t fb = strlen((const char *)b);
    return (fa < fb) - (fa > fb);

}

int main(void){

char pointer[100];
int n;
scanf("%d", &n);
char** strings = malloc(n * sizeof(char*));

int i;
for (i = 0; i < n; i++){
    scanf("%s", pointer);
    strings[i] = malloc(sizeof(char) * (strlen(pointer) + 1));
    strcpy(strings[i], pointer);

}

int m;
scanf("%d", &m);
strings = realloc(strings, (n + m) * sizeof(char*));
for (i = n; i < m + n; i++){

    scanf("%s", pointer);
    strings[i] = malloc(sizeof(char) * (strlen(pointer) + 1));
    strcpy(strings[i], pointer);
}

int a;
int g;
int k = m + n;
qsort(strings , a, 100, sort);
for (g = 0; g < k; g++){
    printf("%s", strings[g]);

    printf("\n");


}
}

You're not calling qsort correctly at all. 您根本没有正确调用qsort

The 2nd parameter is the number of elements in the "array" that you are sorting. 第二个参数是您要排序的“数组”中元素的数量。 You're currently passing it a which as others have pointed out isn't set to anything. 您目前正在传递a它正如其他人指出,没有设置任何东西。 Compiling your code with the "-Wall" option will show you those kinds of errors. 使用“ -Wall”选项编译代码将向您显示这些类型的错误。

The 3rd parameter is the size of one of the elements in the "array", but you've confused this with the size of an unrelated variable pointer ? 第三个参数是“数组”中元素之一的大小,但是您已将其与不相关的变量pointer的大小混淆了? You could write it like sizeof(strings[0]) . 您可以像sizeof(strings[0])这样写。

The finished call should look like qsort(strings,k,sizeof(strings[0]),sort); 完成的调用应类似于qsort(strings,k,sizeof(strings[0]),sort); .

But that still won't work because your sort() function is being passed two pointers to elements in your "array" ( char ** ) and you're treating them as two elements of the "array" ( char * ). 但这仍然不起作用,因为正在向您的sort()函数传递指向“数组”( char ** )中元素的两个指针,并且您将它们视为“数组”( char * )中的两个元素。 So you'd want something like 所以你想要像

size_t fa = strlen(*(char **)a);

Well, this: 好吧,这:

int a;
int g;
int k = m + n;
qsort(strings , a, 100, sort);

makes no sense at all, a has no value so this is undefined behavior. 毫无意义, a没有值,所以这是未定义的行为。

Also sort() is broken, it should just be strcmp() . 同样sort()被破坏了,它应该只是strcmp()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM