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);
}
PaeneInsula
  • 2,010
  • 3
  • 31
  • 57
  • possible duplicate of [How to sort an array of structs in C?](http://stackoverflow.com/questions/8721189/how-to-sort-an-array-of-structs-in-c) – Jonathan Leffler Jan 09 '12 at 01:35
  • Could you provide a self-contained, correct, compilable example with your expected output and actual output? http://sscce.org – David Grayson Jan 09 '12 at 01:38
  • I think your second compare function should work. The first one is wrong though. As David says, you should provide a complete program, input, expected output etc. The explanation may not be the compare function. – David Heffernan Jan 09 '12 at 01:42

1 Answers1

1

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;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Actually the second version in the Q looks like it should work. Why would you expect it to give different results from this? – David Heffernan Jan 09 '12 at 01:38
  • I don't - I didn't notice it until I went to make a working test program, that's all. – Jonathan Leffler Jan 09 '12 at 01:57
  • Yes, that works perfectly. Comparing the two: is the error in my omission of the "const" before dataStream? Thank you for the clear, concise, and courteous help. – PaeneInsula Jan 09 '12 at 02:14
  • 1
    Your second compare function works identically to mine in my framework, so I suspect the problem must have been in your test framework. I modified the initialization code to give a disordered list on initialization, and added the `dump()` code to print it. The initialization might matter; the dumping code doesn't (but the concept of a dump function that takes a file pointer, a tag, and a data structure to be dumped is one I find very useful for many bits of development). – Jonathan Leffler Jan 09 '12 at 02:39