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 6804795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:30:35+00:00 2026-05-26T19:30:35+00:00

I have been stuck on this for a while and nothing seems to work.

  • 0

I have been stuck on this for a while and nothing seems to work.

I have a data structure:

DATA
{
  int size;
  int id;
}

And I have an array of DATA structures:

myArray = (DATA *) malloc(10 * sizeof(DATA));

Then I assign some test values:

myArray[0].size = 5;
myArray[1].size = 9;
myArray[2].size = 1;
myArray[3].size = 3;

So my starting array should look like:

5,9,1,3,0,0,0,0,0,0

Then, I call qsort(myArray,10,sizeof(DATA),comp)

Where comp is:

  int comp(const DATA * a, const DATA * b)
{
    return a.size - b.size;
}

And trust me, I tried many things with the compare function, NOTHING seems to work. I just never get any sorting that makes any sense.

  • 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-26T19:30:35+00:00Added an answer on May 26, 2026 at 7:30 pm

    So my starting array should look like 5, 9, 1, 3, 0, 0, 0, 0, 0, 0.

    No, it really won’t, at least it’s not guaranteed to.

    If you want zeros in there, either use calloc() to zero everything out, or put them in yourself. What malloc() will give you is a block of the size required that has indeterminant content. In other words, it may well have whatever rubbish was in memory beforehand.

    And, on top of that, a and b are pointers in your comp function, you should be using -> rather than . and it’s good form to use the correct prototype with casting.

    And a final note: please don’t cast the return from malloc in C – you can get into problems if you accidentally forget to include the relevant header file and your integers aren’t compatible with your pointers.

    The malloc function returns a void * which will quite happily convert implicitly into any other pointer.


    Here’s a complete program with those fixes:

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct {int size; int id;} DATA;
    
    int comp (const void *a, const void *b) {
        return ((DATA *)a)->size - ((DATA *)b)->size;
    }
    
    int main (void) {
        int i;
        DATA *myArray = malloc(10 * sizeof(DATA));
        myArray[0].size = 5;
        myArray[1].size = 9;
        myArray[2].size = 1;
        myArray[3].size = 3;
        for (i = 4; i < 10; i++)
            myArray[i].size = 0;
    
        qsort (myArray, 10, sizeof(DATA), comp);
        for (i = 0; i < 10; i++)
            printf ("%d ", myArray[i].size);
        putchar ('\n');
    
        return 0;
    }
    

    The output:

    0 0 0 0 0 0 1 3 5 9
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been stuck with this problem for a while now, and I just
I have been stuck on this problem for a while now. I want to
I have been stuck at this for a while. I have been developing a
i've been stuck in this loop for a while. Basically i have a slider
I have been stuck at this for a while. I have a smartgwt widget
I have been stuck on this one for a while - I couldn't figure
Been stuck on this for a while, I have a .dat file with 16
I've been stuck on this for a while now and I can't work out
I have been stuck on this for days, and was wondering if anyone had
Hope to get solution to this problem. I have been stuck on it since

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.