本文介绍了为什么这个qsort()不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Am对字符串数组进行排序(不区分大小写).

Am sorting an array of strings (case insensitive).

qsort导致分段错误,可能是我的转换不正确.

qsort causes segmentation fault, probably my casting isn't proper.

#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 =字符串数组(文件名)

**fileList = array of strings (filenames)

P.S. main()很明显并且可以正常工作.

P.S. main() is obvious and works fine.

推荐答案

我会进行调整,以便您仅对一个简单数组进行排序,在这种情况下,指向char的指针-qsort会安排您获取指针到该数组中的两个元素(即char **指针),需要一些基本的取消引用才能使您找到可通过strcasecmp进行比较的"char指针". @Mark可能已经在您看不见的调用代码中怀疑了260的来源,但是我不是C语言中那种2D数组的忠实拥护者.

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. @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.

以下功能对我来说很重要,并带有一个示例main()来实现它.

The following functions for me, with an example main() to exercise it.

#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;
}

这篇关于为什么这个qsort()不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 22:48