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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:38:29+00:00 2026-06-13T04:38:29+00:00

My code is to sort a structure by just one field. It is strange

  • 0

My code is to sort a structure by just one field.

It is strange that it works when the length is 8 but not in
9.Why is that and what is wrong?

struct node
{
   int key;//I need to sort by the key
   int val;
};
int comp(const void *a, const void *b)
{
return ((struct node *)a)->key > ((struct node *)b)->key;
}
int main()
{
    int i;
    struct node *a;
    a = malloc(10 * sizeof *a);
    /*I have 8 elements*/
    for (i = 0; i < 6; i++)
         a[i].key = 22;
    a[6].key = 21;
    a[7].key = 20;
    a[8].key = 10;
    /*Before sorting, I print it first*/
    for (i = 0; i < 9; i++)
    printf("%3d", a[i].key);
    printf("\n");
    qsort(a, 9, sizeof(struct node), comp);
    /*The sorted answer*/
    for (i = 0; i < 9; i++)
       printf("%3d", a[i].key);
    printf("\n");
    free(a);
    return 0;
}

Output is:

22 22 22 22 22 22 21 20 10

10 22 22 22 22 22 21 20 22

But when I change the length to 8, it works.

  • 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-06-13T04:38:30+00:00Added an answer on June 13, 2026 at 4:38 am

    What do you mean by “length”?

    • You allocate space for 10 elements.
    • You sort 9 of them.
    • You print 9 of them.

    This code is very confusing. You need to have a single well-defined idea of how many elements you’re using, and use the same everywhere. Hint: it should not be a literal number.

    Also, your comparison function is wrong, it needs to return -1, 0 or 1 for elements being less than, equal to, or greater than respectively. Your function wil only return 0 or 1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this sort of code is not best practice, but nevertheless in certain
I'm looking for a library that will disassemble x86 code into some sort of
I've created a javafx project in netbeans. It compiles fine, code completion works...sort off..
I feel sort of lost and overlooking something, but i am not sure how
I have a section of makefile that has this sort of structure: bob: ifdef
Below the code I am using works just fine and outputs the names except
I have this code to sort an array of objects. The data in the
I've a code to sort table using Jquery.I use toggle function to alternate clicks,and
I got this code to sort an unordered list with regards to their title
I'm attempting to do a simple bubble sort code to get familiar with list/string

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.