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

We have a Java Applet built using AWT. This applet lets you select pictures
I have a website built using Asp.net and LinqToSql for Data Access. In a
I have built a web page which contains a Crystal Report built using the
I have a legacy VB6 application that was built using MSDE. As many client's
How do I do the following using built-in modules only? I have a list
I have an ASP.NET app using built-in Membership functionality. As such, I have a
I'm using the built-in SpellCheck for the WPF RichTextBox and would like to ignore
I'm using the built-in Domain Catalog database to list all the databases on a
Is anyone using the built-in code generation provided via text templates (using .tt files)?
I'm using the built in web server in Visual Studio to test some pags

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.