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

  • Home
  • SEARCH
  • 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 3301480
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:45:20+00:00 2026-05-17T20:45:20+00:00

I am using the built in qsort to sort an array of structs. But

  • 0

I am using the built in qsort to sort an array of structs. But after the call to qsort the last element in the array seems to have had its value that I am sorting by set to empty.

Here is my code…

int numEntries = 5;
TvEntry* entries[numEntries]; //create array

//Entries get added to the array here...

qsort( *entries, numEntries, sizeof(TvEntry*), &compareByName ); //sort

displayAll( entries, numEntries ); //display

//here is my sort method
int compareByName( const void* val1, const void* val2 )
{
    const TvEntry* entry1 = (TvEntry*)val1;
    const TvEntry* entry2 = (TvEntry*)val2;
    return strcasecmp( entry1->title, entry2->title );
}   

//here is my display method
void displayAll( TvEntry* entries[], int length )
{
    if( entries == NULL )
    {
        printf( "List is empty\n" );
    }
    else
    {
        int i = 0;
        for( i = 0; i < length; i++ )
        {
            printf( "ENTRY: %s\n", entries[i]->title );
        }
    }
}

I should mention that if i comment out the line which calls qsort then all entries are displayed correctly but when qsort is called then the entries are all displayed (Not Sorted) and printing a blank value for title for the last entry.

  • 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-17T20:45:20+00:00Added an answer on May 17, 2026 at 8:45 pm
     qsort( *entries, numEntries, sizeof(TvEntry*), &compareByName);
    

    is wrong, you don’t want to dereference the first element of your array. Make it

     qsort(entries, numEntries, sizeof(TvEntry*), &compareByName);
    

    Furthermore, your compare funciton will receive pointers to the elements, and the elements are pointers, so it should be e.g.

    int compareByName( const void* val1, const void* val2 )
    {
        const TvEntry** entry1 = (TvEntry**)val1;
        const TvEntry** entry2 = (TvEntry**)val2;
        return strcasecmp( (*entry1)->title, (*entry2)->title );
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the in-built function qsort() to sort a vector of class item
How do I do the following using built-in modules only? I have a list
MVC3 vb.net Application using built in session management. I have an mvc3 application that
I have a requirement such that I need to add two integers using built
I try to make a little program that sorts an array using threads but
Im trying to run a VOIP call using built in SIP on android 3.1.
I have an ASP.NET app using built-in Membership functionality. As such, I have a
I have inherited an app set up using built using rails using the facebooker
XCode 3.1.2 using built-in OCTest unit testing. I'm getting a crash when unit tests
I am using built in Drupal 6 user module, fore user registration, forgot-your-password-emails and

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.