简体   繁体   English

C qsort 嵌套结构数组

[英]C qsort array of nested structs

I'm trying to sort an array of struct records.我正在尝试对结构记录数组进行排序。 For some reason a core dump keeps occuring.出于某种原因,核心转储不断发生。

When I try doing the same thing with an array of ints or structs, it works perfectly fine.当我尝试用整数或结构数组做同样的事情时,它工作得非常好。 However when I start using nested structs, it begins to core dump.但是,当我开始使用嵌套结构时,它开始进行核心转储。

The current output is:当前输出为:

Before sorting
first last 0  
first last 1  
first last 2  
first last 3  
first last 4  
first last 5  

AFTER sorting  
Segmentation fault (core dumped)

Compiler: Cygwin编译器:Cygwin

typedef struct {
    char last[NAMESIZE]; /* last name (1 word) */
    char first[NAMESIZE]; /* first name (1 word) */
} name;

typedef struct {
    name name;
    int score; /* score (between 0 & 100 inclusive) */
} record;

int compare (const void * a, const void * b){
    const record *recordA = (record *)a;
    const record *recordB = (record *)b;
    printf("%d: %d", recordA->score, recordB->score);
    return ( recordB->score - recordA->score );
}

int main (){
    record ** list;
    int i;
    list=malloc(6*sizeof(record*));
    printf("Before sorting\n");
    for(i=0; i<6; i++){ 
        list[i]=malloc(sizeof(record));
        strcpy(list[i]->name.first,"first");
        strcpy(list[i]->name.last,"last");
        list[i]->score=i;   
    }
    for (i=0; i<6; i++){
        printf ("%s %s %d\n",list[i]->name.first, list[i]-    >name.last,list[i]->score);         
    }

    printf("AFTER sorting\n");
    qsort (list, 6, sizeof(record), compare);
    for (i=0; i<6; i++){
        printf ("%s %s %d\n",list[i]->name.first, list[i]- >name.last,list[i]->score);         
    }    
  return 0;
}

list is an array of 6 pointers to record : list是一个包含 6 个指向record指针的数组:

list=malloc(6*sizeof(record*));

so you need to pass the same size to qsort .所以你需要将相同的大小传递给qsort

qsort (list, 6, sizeof(record), compare);

should be应该

qsort (list, 6, sizeof(record*), compare);

or better yet或者更好

qsort (list, 6, sizeof(*list), compare);

I'm agree with the first answer.But one thing clear is that you should not use qsort (list, 6, sizeof(record), compare) , in which sizeof(record) will return the total bytes of record but do not automatically count the bytes used by the name.我同意第一个答案。但有一点很清楚,你不应该使用 qsort (list, 6, sizeof(record), compare) ,其中 sizeof(record) 将返回记录的总字节数,但不会自动返回计算名称使用的字节数。 nor qsort (list, 6, sizeof(record), compare) , in which sizeof(record) will return the 4 bytes of record*,since it's a pointers.也不是 qsort (list, 6, sizeof(record), compare) ,其中 sizeof(record) 将返回 record* 的 4 个字节,因为它是一个指针。 Instead, I think maybe you just need count the bytes by your self and add another data,like namesize, in your data structure record相反,我认为您可能只需要自己计算字节数并在数据结构记录中添加另一个数据,例如名称大小

If you have a comparison function for two records, you want to assign them to a pointer to a constant that points to a record.如果您有两个记录的比较函数,您希望将它们分配给一个指向指向记录的常量的指针。

In your qsort, I'm guessing you've passed in your record list data structure, so the number of elements will be your nused.在您的 qsort 中,我猜您已经传入了您的记录列表数据结构,因此元素的数量将被您使用。

/*Comparison function: Score ascending only.*/
int cmp_sasc(const void *p, const void *q){
    record * const *pp = p;
    record * const *qq = q;
    return (*pp)->score - (*qq)->score;
}

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

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