Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7030983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:44:35+00:00 2026-05-28T00:44:35+00:00

There are lots of questions on stackoverflow regarding how to sort an array of

  • 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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:44:36+00:00Added an answer on May 28, 2026 at 12:44 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, I noticed there are many questions regarding this, lots marked as duplicate. I
There are lots of questions that ask this or a similar question. They all
There are LOTS of iphone-related questions of this type. I know; I read them
There are lots of similar questions to this, but they all seem to involve
So there are lots of posts on StackOverflow regarding this, but I still was
There are lots of answers on SO for similar questions, which all describe how
There are lots of questions on how to improve communication between teams. One way
There are lots of questions on how to strip html tags, but not many
There are lots of questions about how to force the browser to cache or
There are already lots of questions on SO about exceptions, but I can't find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.