简体   繁体   English

如何快速排序指向C语言中结构的指针的数组?

[英]How to quick sort an array of pointers to structures in C?

So there are lots of examples of using qsort() with structures, pointers, etc. But none of them seem to sort correctly in my implementation. 因此,有很多将qsort()与结构,指针等一起使用的示例。但是在我的实现中,似乎没有一个排序正确。

This is an overview of my code: 这是我的代码的概述:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node {
  int value;
};
typedef struct node Node;

int compare(const void *p1, const void *p2);

int main()
{
    Node *myArray[10];
    Node *node1, *node2, *node3;
    int i;

    node1 = (Node *)malloc(sizeof(Node));
    node1->value = 10;
    myArray[0] = node1;

    node2 = (Node *)malloc(sizeof(Node));
    node2->value = 7;
    myArray[1] = node2;

    node3 = (Node *)malloc(sizeof(Node));
    node3->value = 12;
    myArray[2] = node3;

    for (i=0; i<3; i++) {
        printf("Element %i: %i\n", i, myArray[i]->value);
    }

    printf("-------------\n");

    qsort(myArray, 3, sizeof(Node*), compare);

    for (i=0; i<3; i++) {
        printf("Element %i: %i\n", i, myArray[i]->value);
    }

    return 0;
}

int compare(const void *p1, const void *p2)
{
    Node *node1 = (Node*) p1;
    Node *node2 = (Node*) p2;

    return node1->value - node2->value;
}

This code is to demonstrate my issue, so please don't have a rant at me for semantics! 这段代码是用来演示我的问题的,所以请不要对我的语义大加指责! The extra unused space in the array is intentional. 阵列中多余的未用空间是有意的。 :p :p

So as far as I'm aware, and from what I'm reading online, this should work. 据我所知以及从我在网上阅读的内容来看,这应该可行。 But it doesn't. 但事实并非如此。 For some reason it starts sorting through garbage values in the compare function. 出于某种原因,它开始通过垃圾值比较功能排序。

I require the array to often be larger than the values within it, so hoped the second argument of the qsort() function would limit it to only the first 3 elements in this case. 我所需要的阵列通常比在其内的值越大,所以希望的第二个参数qsort()函数将它限制在仅在这种情况下,第一3个元素。 But it seems to be ignoring that. 但这似乎忽略了这一点。

Any ideas what's causing this strange behaviour? 有什么想法导致这种奇怪的行为吗?

When you pass *myArray as the first argument to the qsort function, it's like passing myArray[0] , which is definitely not the correct pointer (and will lead to undefined behavior and most likely some weird behavior like sorting "garbage" data). 当将*myArray作为第一个参数传递给qsort函数时,就好像传递了myArray[0] ,这绝对不是正确的指针(这将导致未定义的行为,并且很可能会产生一些奇怪的行为,例如对“垃圾”数据进行排序)。

Either let the array decay to a pointer to the first element by using plain myArray , or explicitly specify the first element using &myArray[0] . 通过使用纯myArray让数组衰减到指向第一个元素的指针,或者使用&myArray[0]显式指定第一个元素。

Joachim has solved one problem , there's another. Joachim解决了一个问题 ,还有另一个问题 qsort passes pointers to the array elements to compare even if they are already pointers . qsort指针传递给数组元素以compare 即使它们已经是指针 So compare is getting Node ** , a double pointer. 因此, compare得到的是双指针Node ** You can see this if you print node->value inside compare . 如果在compare打印node->value则可以看到此内容。

Cast accordingly. 进行相应的投放。

static int compare(const void *p1, const void *p2)
{
    const Node *node1 = *(const Node **)p1;
    const Node *node2 = *(const Node **)p2;

    printf("cmp %d %d\n", node1->value, node2->value);
    return node1->value - node2->value;
}

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

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