[英]qsort an array of structs in a struct
I'm attempting to sort an array of structs in a struct based on an int value. 我试图基于int值对结构中的结构数组进行排序。 I've successfully sorted an array of structs but I'm guessing I'm passing a wrong value somewhere for the nested structs. 我已经成功地对结构体数组进行了排序,但是我猜想我在某个地方为嵌套结构体传递了错误的值。
I just need the structs in the array sorted the value of a. 我只需要数组中的结构对a的值进行排序。
The structs are set up as: 结构设置为:
struct s2{
int a;
int b;
};
struct s1{
int c;
struct s2 arr[10];
}
I have a compare function: 我有一个比较功能:
int comp(const void *a, const void *b){
struct s1 *q1 = (struct s1 *)a;
struct s1 *q2 = (struct s1 *)b;
return(q1->arr->a - q2->arr->a);
}
And I call qsort: 我打电话给qsort:
struct s1 myStruct;
size_t theLen = sizeof(myStruct.arr) / sizeof(struct s2);
qsort(myStruct.arr, 10, theLen, comp);
For the input: 对于输入:
10, 5, 7, 20, 17, 9, 3, 11, 15, 1
I get output: 我得到输出:
2147451181, 589824, 327680, 65536, 131072, 4, 5, 11, 15, 8
I'm guessing it may be something to do with how I declare the length? 我猜这可能与我声明长度有关吗?
Thanks! 谢谢!
The file line is: 文件行是:
10 5 7 20 17 9 3 11 15 1
myStruct.arr[i].a is filled from file input using fgets and sscanf: 使用fgets和sscanf从文件输入中填充myStruct.arr [i] .a:
fgets(t, sizeof(t), fp);
sscanf(t, "%d,...,%d", &myStruct.arr[0].a,...,&myStruct.arr[9].a);
myStruct.arr[i].b is filled with a for loop: myStruct.arr [i] .b充满了for循环:
for(int i = 0; i < 10; i++){
myStruct.arr[i].b = i+1;
}
qsort(myStruct.arr, 10, theLen, comp);
You are sorting myStruct.arr
here where each element is of type struct s2
.So your compare should be 您在这里对myStruct.arr
进行排序,其中每个元素的类型均为struct s2
。因此,您的比较应该为
int comp(const void *a, const void *b){
struct s2 *q1 = (struct s2 *)a;
struct s2 *q2 = (struct s2 *)b;
return(q1->a - q2->a);
}
EDIT: the third parameter to qsort
is the size of each element of array to be sorted.So it should be 编辑: qsort
的第三个参数是要排序的数组的每个元素的大小,因此它应该是
qsort(myStruct.arr, theLen, sizeof(struct s2), comp);
There are two mistakes with your code 您的代码有两个错误
You are using q1->arr->a
to compare where you should use q1->a
(where q1
is of type const struct s2
). 您正在使用q1->arr->a
来比较应该使用q1->a
(其中q1
是const struct s2
类型)。 This has also been explained by @GauravSehgal in his answer @GauravSehgal在回答中也对此进行了解释
If you look at the third argument to qsort , it is acutally the size of each element to compare in bytes. 如果查看qsort的第三个参数,则它实际上是要比较的每个元素的大小(以字节为单位)。 But you have passed the number of elements. 但是您已经通过了元素数量。 Change your call to - 将通话更改为-
qsort(myStruct.arr, 10, sizeof(struct s2), comp);
and you should get the desired result. 并且您应该得到期望的结果。
There are some other points you need to take care of (pointed out by @Stargateur) - 还有一些其他需要注意的地方(@Stargateur指出)-
Declare q1
and q2
to be of type const struct s2*
, because you do not want to discard the const
qualifier. 将q1
和q2
声明为const struct s2*
类型,因为您不想丢弃const
限定符。
Do not cast a
and b
explicitly while assigning to q1
and q2
because they are of type const void*
which auto-promotes to pointer of any const type. 分配给q1
和q2
时,请勿显式转换a
和b
,因为它们的类型为const void*
,它会自动提升为任何const类型的指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.