简体   繁体   English

为什么这个qsort()不起作用?

[英]Why this qsort() doesn't work?

Am sorting an array of strings (case insensitive). 正在对字符串数组进行排序(不区分大小写)。

qsort causes segmentation fault, probably my casting isn't proper. qsort导致分段错误,可能是我的转换不正确。

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

int compare(const void *string1, const void *string2) {
    char *a = (char*)(string1);
    char *b = (char*)(string2);
    printf("comparing %s     AND    %s\n", a, b);
    return strcasecmp(a,b);
}

void sortListName(char **fileList, int noOfFiles) {
    printf("Sorting\n");
    qsort(fileList, noOfFiles, 260*sizeof(char), compare); 
    return;     
}

**fileList = array of strings (filenames) ** fileList =字符串数组(文件名)

PS main() is obvious and works fine. PS main()很明显并且可以正常工作。

If this is all the code you have related to the qsort , it looks like you declared the comparePtr function pointer, but it's still not initialized; 如果这是与qsort相关的所有代码,则好像已声明了comparePtr函数指针,但仍未初始化。 it's not pointing to your compare function (which is what I assume you wanted it to point to). 它没有指向您的compare功能(这是我想您要它指向的功能)。

After that, a few more things: 在那之后,还有几件事:

1) comparePtr has the correct types, but compare does not. 1) comparePtr具有正确的类型,但compare没有。 It needs to take in two const void* , but you have two const void** . 它需要接受两个const void* ,但是您有两个const void**
2) Once you fix the types, you could just pass compare to qsort , instead of making a function pointer and passing that. 2)修复类型后,您可以将compare传递给qsort ,而不是创建函数指针并将其传递。
3) I'm not convinced the first argument to qsort is correct. 3)我不相信qsort的第一个参数是正确的。 You want to be passing in the pointer to the first element in the array, which ought to just be fileList (I'm assuming it points to the first string in your array). 您想要传递指向数组中第一个元素的指针,该指针应该只是fileList (我假设它指向数组中的第一个字符串)。
4) The third argument isn't correct either. 4)第三个参数也不正确。 fileList is a char** , meaning you're passing in an array of char * s, and hence the third argument should just be sizeof(char*) , not the strlen s of the strings. fileList是一个char** ,这意味着您要传入一个char * s数组,因此第三个参数应该只是sizeof(char*) ,而不是字符串的strlen

I would adjust things so that you're just sorting a simple array, in this case of pointers to char - qsort will arrange for you to get pointers to two elements in that array (that is, char ** pointers), and some basic dereferencing is needed to get you to the "pointers to char" comparable via strcasecmp . 我会进行调整,以便您只对一个简单的数组进行排序,在这种情况下,指向char的指针qsort将安排您获取指向该数组中两个元素的指针(即char **指针),以及一些基本的需要取消引用才能使您到达可通过strcasecmp比较的“ char指针”。 @Mark likely has sussed out the source of the 260 in your unseen calling code, but I'm not a big fan of those kinds of 2d arrays in C. @Mark可能已经在您看不见的调用代码中怀疑了260的来源,但是我不是C语言中那种2d数组的忠实拥护者。

The following functions for me, with an example main() to exercise it. 以下函数对我有用,并带有一个示例main()来执行它。

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


int compare(const void *v1, const void *v2){
    char *a = *(char **)v1;
    char *b = *(char **)v2;

    printf("comparing %s to %s\n", a, b);

    return strcasecmp(a,b);
}


void sortListName(char **fileList, int noOfFiles){
    printf("Sorting\n");
    qsort(fileList, noOfFiles, sizeof(*fileList), compare); 
    return;

}

int
main(void)
{
    char *filenames[] = {
        "/var/www/icons/comp.gray.png",
        "/var/www/error/HTTP_SERVICE_UNAVAILABLE.html.var",
        "/var/www/icons/right.gif",
        "/var/www/error/HTTP_NOT_IMPLEMENTED.html.var",
        "/var/www/icons/pie3.png",
        "/var/www/icons/pie2.png",
        "/var/www/htdocs/manual/mod/mod_proxy_balancer.html",
        "/var/www/htdocs/manual/programs/rotatelogs.html",
        "/var/www/htdocs/manual/vhosts/mass.html",
        "/var/www/icons/movie.png",
        "/var/www/htdocs/manual/images/caching_fig1.png",
        "/var/www/htdocs/htdig/search.html",
        "/var/www/icons/generic.gif",
        "/var/www/htdocs/manual/mod/quickreference.html",
        "/var/www/icons/small/blank.png",
        "/var/www/icons/image2.gif"
    };

    int i, nf = (int) (sizeof(filenames) / sizeof(filenames[0]));

    puts("Unsorted:");

    for (i = 0; i < nf; i++) {
        puts(filenames[i]);
    }

    sortListName(filenames, nf);

    puts("Sorted:");

    for (i = 0; i < nf; i++) {
        puts(filenames[i]);
    }

    return 0;
}

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

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