在c中的结构指针数组上使用qsort时比较整数的函数

发布于 2024-12-25 18:15:47 字数 1363 浏览 2 评论 0原文

stackoverflow 上有很多关于如何对结构指针数组进行排序的问题。我把它们都翻遍了,没有结果。我想对指向结构数组的指针数组进行排序。我首先为指针数组分配存储空间,然后为结构本身分配存储空间。一切看起来都很好,但我无法将它们排序。我确信问题出在比较函数中。我从 stackoverflow 复制了其中一些,它们列在下面。但它们都不起作用...

typedef struct s_stream{
int amc;
char *name;
} dataStream;

void abc(void)
{
       int count = 100;

    dataStream *_UniqueStreamBuild  = calloc(count, sizeof(dataStream ));
    dataStream **UniqueStreamBuild =  calloc(count, sizeof(dataStream *));
    for ( int i = 0; i < count; ++i) UniqueStreamBuild[i] = _UniqueStreamBuild + i; 

//**Edit: ******** **
        // here I call a cascade of functions that assign values to amc; those
        // functions are correct: they produce an unsorted array of amc values;
        // the output I am getting is an array of structures seemingly in random order.

    qsort(UniqueStreamBuild, count, sizeof(dataStream *), compare);  
}

int compare (const void * a, const void * b)
{
    const dataStream *x = a;
    const dataStream *y = b;

    if (x->amc > x->amc)
      return(1);

  if (x->amc < x->amc)
      return(-1);

  return(0);   
}


int compare( const void *a, const void *b )
{
  dataStream *m1 = *(dataStream **)a;
  dataStream *m2 = *(dataStream **)b;

  if (m1->amc > m2->amc)
      return(1);

  if (m1->amc < m2->amc)
      return(-1);

  return(0);
}

There are lots of questions on stackoverflow regarding how to sort an array of structure pointers. I looked through them all, to no avail. I want to sort an array of pointers to an array of structures. I first allocate storage for the pointer array, then for the structures themselves. All that seems fine, but I can't get them sorted. I'm sure the problem is in the compare function. I've copied a few of them from stackoverflow, and they are listed below. But none of them work...

typedef struct s_stream{
int amc;
char *name;
} dataStream;

void abc(void)
{
       int count = 100;

    dataStream *_UniqueStreamBuild  = calloc(count, sizeof(dataStream ));
    dataStream **UniqueStreamBuild =  calloc(count, sizeof(dataStream *));
    for ( int i = 0; i < count; ++i) UniqueStreamBuild[i] = _UniqueStreamBuild + i; 

//**Edit: ******** **
        // here I call a cascade of functions that assign values to amc; those
        // functions are correct: they produce an unsorted array of amc values;
        // the output I am getting is an array of structures seemingly in random order.

    qsort(UniqueStreamBuild, count, sizeof(dataStream *), compare);  
}

int compare (const void * a, const void * b)
{
    const dataStream *x = a;
    const dataStream *y = b;

    if (x->amc > x->amc)
      return(1);

  if (x->amc < x->amc)
      return(-1);

  return(0);   
}


int compare( const void *a, const void *b )
{
  dataStream *m1 = *(dataStream **)a;
  dataStream *m2 = *(dataStream **)b;

  if (m1->amc > m2->amc)
      return(1);

  if (m1->amc < m2->amc)
      return(-1);

  return(0);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪 2025-01-01 18:15:47

您的第二个可能的 compare() 函数应该可以工作,除非它与下面的这个版本之间存在一些我没有注意到的差异。对指针数组进行排序时,比较函数会传递两个指向 dataStream * 的指针,因此比较器应该与此非常相似:

int compare (const void *a, const void *b)
{
    const dataStream *x = *(const dataStream **)a;
    const dataStream *y = *(const dataStream **)b;

    if (x->amc > y->amc)
        return(1);
    else if (x->amc < y->amc)
        return(-1);
    else
        return(0);   
}

此外,正如最初所写,您的函数之一始终返回 0,因为x->amc == x->amc (取消引用 x 两次,而不是 xy >)。

您的测试代码没有完全初始化数据结构 - 它使用calloc(),因此结构中的字符串和指针都归零,因此排序作用不大。


这段代码对我有用...你呢?

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

typedef struct s_stream
{
    int   amc;
    char *name;
} dataStream;

static int compare(const void *a, const void *b)
{
    const dataStream *x = *(const dataStream **)a;
    const dataStream *y = *(const dataStream **)b;

    if (x->amc > y->amc)
        return(1);
    else if (x->amc < y->amc)
        return(-1);
    else
        return(0);
}

static void dump(FILE *fp, const char *tag, dataStream * const * const data, int num)
{
    const char *pad = "";
    fprintf(fp, "Stream Dump (%s): (%d items)\n", tag, num);
    for (int i = 0; i < num; i++)
    {
        fprintf(fp, "%s%d", pad, data[i]->amc);
        if (i % 10 == 9)
        {
            putc('\n', fp);
            pad = "";
        }
        else
            pad = ", ";
    }
    putc('\n', fp);
}

static void abc(void)
{
    int count = 100;

    dataStream *_UniqueStreamBuild  = calloc(count, sizeof(dataStream ));
    dataStream **UniqueStreamBuild =  calloc(count, sizeof(dataStream *));
    for ( int i = 0; i < count; ++i)
    {
        UniqueStreamBuild[i] = _UniqueStreamBuild + i;
        UniqueStreamBuild[i]->amc = (7 * i + 3) % count + 1;
    }

    dump(stdout, "Before", UniqueStreamBuild, count);
    qsort(UniqueStreamBuild, count, sizeof(dataStream *), compare);
    dump(stdout, "After", UniqueStreamBuild, count);

    free(_UniqueStreamBuild);
    free(UniqueStreamBuild);
}

int main(void)
{
    abc();
    return 0;
}

Your second possible compare() function should work unless there's some difference I didn't notice between it and this version just below. When sorting an array of pointers, the comparison function is passed two pointers to a dataStream *, hence the comparator should be very similar to this:

int compare (const void *a, const void *b)
{
    const dataStream *x = *(const dataStream **)a;
    const dataStream *y = *(const dataStream **)b;

    if (x->amc > y->amc)
        return(1);
    else if (x->amc < y->amc)
        return(-1);
    else
        return(0);   
}

Also, as originally written, one of your functions always returns 0 because x->amc == x->amc (you dereference x twice, instead of x and y).

Your test code does not fully initialize the data structures - it uses calloc() so the strings and pointers in the structures are all zeroed, so sorting doesn't do much.


This code works for me...how about you?

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

typedef struct s_stream
{
    int   amc;
    char *name;
} dataStream;

static int compare(const void *a, const void *b)
{
    const dataStream *x = *(const dataStream **)a;
    const dataStream *y = *(const dataStream **)b;

    if (x->amc > y->amc)
        return(1);
    else if (x->amc < y->amc)
        return(-1);
    else
        return(0);
}

static void dump(FILE *fp, const char *tag, dataStream * const * const data, int num)
{
    const char *pad = "";
    fprintf(fp, "Stream Dump (%s): (%d items)\n", tag, num);
    for (int i = 0; i < num; i++)
    {
        fprintf(fp, "%s%d", pad, data[i]->amc);
        if (i % 10 == 9)
        {
            putc('\n', fp);
            pad = "";
        }
        else
            pad = ", ";
    }
    putc('\n', fp);
}

static void abc(void)
{
    int count = 100;

    dataStream *_UniqueStreamBuild  = calloc(count, sizeof(dataStream ));
    dataStream **UniqueStreamBuild =  calloc(count, sizeof(dataStream *));
    for ( int i = 0; i < count; ++i)
    {
        UniqueStreamBuild[i] = _UniqueStreamBuild + i;
        UniqueStreamBuild[i]->amc = (7 * i + 3) % count + 1;
    }

    dump(stdout, "Before", UniqueStreamBuild, count);
    qsort(UniqueStreamBuild, count, sizeof(dataStream *), compare);
    dump(stdout, "After", UniqueStreamBuild, count);

    free(_UniqueStreamBuild);
    free(UniqueStreamBuild);
}

int main(void)
{
    abc();
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文