简体   繁体   English

C:使用qsort对指向ADT的指针数组进行排序(结构)

[英]C: Using qsort to sort an array of pointers to an ADT (struct)

I've been trying to use qsort() to sort an array of pointers of an ADT (named SSET ), but I'm getting a weird result. 我一直在尝试使用qsort()对ADT的指针数组(名为SSET )进行排序,但结果SSET奇怪。 Here's my function: 这是我的功能:

extern void sset_sort_sets(SSET *sets[], int n) {
    qsort(sets, n, sizeof(SSET *), sset_cmp);
}

In sset_cmp() , I put a statement to print out the SSET s it is comparing. sset_cmp() ,我放置了一条语句以打印出正在比较的SSET

int sset_cmp(const void *a, const void *b){
    SSET *pa = (SSET *) a;
    SSET *pb = (SSET *) b;

    printf("sset_cmp called:\n");
    printf("  a = "); sset_display(pa);
    printf("  b = "); sset_display(pb);
    printf("\n");

    // More stuff...

I created an array of two SSET s (in main() ) 我创建了一个由两个SSET的数组(在main()

SSET *s[2];
int a[] = {-4, 0, 3, 3, 22};
int c[] = { 1, 2, 3, 4, 5};
s[0] = sset_from_array(c, n); // { 1 2 3 4 5 }
s[1] = sset_from_array(a, n); // { -4 0 3 22 }

int i;
printf("Before sort:\n");
for (i = 0; i < 2; i++)
    sset_display(s[i]);
printf("\n");

sset_sort_sets(s, 2);

printf("After sort\n");
for (i = 0; i < 2; i++)
    sset_display(s[i]);

But here is what I get in the output: 但是这是我在输出中得到的:

Before sort:
  { 1 2 3 4 5 }
  { -4 0 3 22 }

sset_cmp called:
  a =   { -1589621936 -4 0 3 22 }
  b =   { -1589621568 }

After sort
  { 1 2 3 4 5 }
  { -4 0 3 22 }

I think I'm messing something up with the pointers in calling qsort() , but I'm not sure how. 我想我在调用qsort()弄乱了指针,但不确定如何。

I think I included all of the related code. 我想我包括了所有相关代码。 Tell me if there's any other segments you need to find my mistake. 告诉我是否还需要其他细分来发现我的错误。

SSET *pa = (SSET *) a;

looks wrong. 看起来错了。 qsort passes pointers to the elements to the comparison function. qsort将指向元素的指针传递给比较函数。 Your array elements are pointers, so your comparison function receives pointers to pointers: 您的数组元素是指针,因此您的比较函数将接收指向指针的指针:

SSET *pa = *(SSET **)a;

qsort() passes a pointer to the array elements into the comparison function. qsort()将指向数组元素的指针传递给比较函数。 So, if you have an array of int , the comparison function will receive int * . 因此,如果您有一个int数组,则比较函数将接收int *

You are attempting to sort an array of pointers to SSET , that is, an array of SSET * . 您正在尝试对指向SSET的指针数组(即SSET *数组)进行SSET * Thus, the comparison function will receive pointers to pointers to SSET (so, SSET ** ). 因此,比较函数将接收指向SSET指针(即SSET ** )。

So you need to change this: 因此,您需要更改以下内容:

SSET *pa = (SSET *) a;
SSET *pb = (SSET *) b;

To: 至:

SSET *pa = *(SSET **) a;
SSET *pb = *(SSET **) b;

Also, it is usually a good idea not to hardcode a typename as the operand of sizeof . 另外,通常最好不要将类型名称硬编码为sizeof的操作数。 If the type changes, it is another place you'll have to update in the future, and you might make mistakes because you have to remember to use the correct type. 如果类型更改,则将来必须在另一个位置进行更新,并且可能会犯错,因为您必须记住使用正确的类型。

Consider changing this line: 考虑更改此行:

qsort(sets, n, sizeof(SSET *), sset_cmp);

To: 至:

qsort(sets, n, sizeof(sets[0]), sset_cmp);

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

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