看来我的qsort没有给出正确的结果为什么?

Cod*_*elf 1 c qsort

这是我的比较功能:

   int compare (const void * a, const void * b)
    {
        ptnode * ia = (ptnode*)a;
        ptnode * ib = (ptnode*)b;
        return (int)(100.f*ia->x - 100.f*ib->x );
    }
Run Code Online (Sandbox Code Playgroud)

我打电话给qsort:

qsort(sortbase,index,sizeof(ptnode),compare);
Run Code Online (Sandbox Code Playgroud)

sortbase是我的struct ptnode的数组,定义如下:

typedef struct node
{
    struct node  *pre1;
    struct node  *pre2;
    struct node  *pre;
    double          x;
    double y;
    double maxlength;
} ptnode;
Run Code Online (Sandbox Code Playgroud)

sortbase是这样的:

struct node * sortbase[1000];
Run Code Online (Sandbox Code Playgroud)

我想按x值对它们进行排序,但在qsort之前和之后,没有任何改变,

为什么?提前致谢.

nos*_*nos 5

compare函数接收指向您需要比较的2个元素的指针.由于您的元素是指针,因此compare函数需要处理指针指针.

int compare (const void * a, const void * b)
{
    ptnode * ia = *(ptnode**)a;
    ptnode * ib = *(ptnode**)b;
    return (int)(100.f*ia->x - 100.f*ib->x );
}
Run Code Online (Sandbox Code Playgroud)