简体   繁体   English

结构排序数组

[英]Sorting array of structs

I have defined an array of structs 我定义了一个结构数组

typedef struct sorting {
    int number
} SRT;

SRT *mystr = NULL;

which I later dynamically allocated. 我后来动态分配了。 and I want to sort it by the number int; 我想按number int排序;

What kind of function do I have to write in order for qsort to do it? 我必须编写哪种函数才能使qsort做到这一点? I have written : 我已经写了 :

qsort(mystr,array_index,sizeof(mystr),magic);

int magic(const void *a, const void *b) {
    int one=((const struct mystr*)a)->number;
    int two(( const struct myst*)b)->number;

    return ( one-two);
}

but it didn't work. 但这没用。 How can I do it? 我该怎么做? it throwed errors about not naming a type. 它引发了有关不命名类型的错误。

You cannot reliably sort the array with the function as written: 您不能使用编写的函数可靠地对数组进行排序:

  • It has syntax errors, some of which are typos, others indicate a confusion between types, struct tags and variable names. 它具有语法错误,其中一些是拼写错误,其他则表明类型,结构标记和变量名之间存在混淆。

  • return (one - two); only works for reasonably small values of one and two . 仅适用于较小的onetwo值。 It will invoke undefined behavior if there is an integer arithmetic overflow. 如果存在整数算术溢出,它将调用未定义的行为。 For example, if one == INT_MAX and two == -1 , the value of one - two is not specified by the C language and on common platform it is likely to return INT_MIN , a negative value, leading to incorrect sorting. 例如,如果one == INT_MAXtwo == -1 ,价值one - two不是由C语言和共同的平台指定,它很可能会回到INT_MIN ,负值,从而导致不正确的排序。

A simple solution is this: 一个简单的解决方案是这样的:

int sort_function(const void *a, const void *b) {
    int one = ((const SRT*)a)->number;
    int two = ((const SRT*)b)->number;

    return (one > two) - (one < two);
}

The expression (one > two) - (one < two) evaluates to -1 if one is less than two , 0 if they are equal and 1 otherwise. 如果one小于two ,则表达式(one > two) - (one < two)-1 ,如果相等则为0 ,否则为1 In C, comparisons evaluate to 0 if false and 1 if true. 在C语言中,如果为false,则比较结果为0如果为true,则比较结果为1

The sorting function should be used this way: 排序功能应通过以下方式使用:

qsort(mystr, array_count, sizeof(*mystr), sort_function);
  • The second argument is the number of structures in the array pointed to by mystr . 第二个参数是mystr指向数组中结构的数量。
  • The third argument is the size of a single structure: sizeof(mystr) is the size of the pointer, not the size of what is points to. 第三个参数是单个结构的大小: sizeof(mystr)是指针的大小,而不是指向的对象的大小。
  • Avoid mysterious names like magic ... use descriptive names for types, functions and variables. 避免使用诸如magic类的神秘名称...对类型,函数和变量使用描述性名称。

Two problems: 两个问题:

qsort(mystr,array_index,sizeof(mystr),magic);

mystr is a pointer to SRT , so you're passing in the size of a pointer to the struct, not the size of the struct: mystr是指向SRT的指针,因此您要传递的是指向结构的指针的大小,而不是结构的大小:

qsort(mystr,array_index,sizeof(STR),magic);

Then there's this: 然后是这样的:

int one=((const struct mystr*)a)->number;
int two(( const struct myst*)b)->number;

mystr is a variable name, not a type, and myst isn't defined anywhere. mystr是变量名,而不是类型,并且myst在任何地方都没有定义。 You need the type name here: 您需要在此处输入类型名称:

int one=((const SRT *)a)->number;
int two=((const SRT *)b)->number;

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

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