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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:47:00+00:00 2026-05-31T18:47:00+00:00

I am attempting to understand the c library qsort in the context of pointers

  • 0

I am attempting to understand the c library qsort in the context of pointers to structs.
Here is the existing code that I would like to manipulate:

The structure:

#define MAX_NAME 20
#define NUM_MONTHS 12

typedef struct EMP {
    char name[MAX_NAME+1];
    int monthSales[NUM_MONTHS];
    int total;
} Emp;

The global initialization of the data and its size:

Emp *data;//where all entries are kept
int empSize;

and I have constructed 2 arrays of Emp pointers that I would like refer to the data in different orders:

Emp *nameArray[empSize];//an array of pointers to point to entries alphabetically
Emp *salesArray[empSize]; //an array of pointers to pointing to entries by sales

after they have been identically assigned, I would like to use a qsort to arrange them differently.
the nameArray alphabetically, using the name in the struct and
the salesArray largest to smallest, using the total in the struct

What should the compare methods and the qsort arguments look like?

Thanks

  • 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-31T18:47:01+00:00Added an answer on May 31, 2026 at 6:47 pm

    You just need to define two different comparison functions. Each comparison function should take in two pointers to void (in this case, you would cast them to Emp ** types) and then return a negative integer, zero, or a positive integer if the first entry is less than, equal to, or greater than the second one, respectively.

    For the total-based sort, you can simply subtract the second total from the first. If the first total is less than the second, this results in a negative number, and the opposite is true when the first total is greater than the second. When they are equal, zero is returned.

    int compareByTotal(const void *first, const void *second)
    {
        int firstTotal = (*(Emp **)first)->total;
        int secondTotal = (*(Emp **)second)->total;
    
        return firstTotal - secondTotal;
    }
    

    The second one, since it’s a string comparison, can return the value of a strcmp (which obeys the same return value conventions):

    int compareByName(const void *first, const void *second)
    {
        const char *firstName = (*(Emp **)first)->name;
        const char *secondName = (*(Emp **)second)->name;
    
        return strcmp(firstName, secondName);
    }
    

    Then you can just call qsort passing in those function names:

    /* given: */
    Emp *nameArray[empSize];//an array of pointers to point to entries alphabetically
    Emp *salesArray[empSize]; //an array of pointers to pointing to entries by sales
    
    /* use: */
    qsort(nameArray, empSize, sizeof(*nameArray), &compareByName);
    qsort(salesArray, empSize, sizeof(*salesArray), &compareByTotal);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Any known C# library or project out there that can understand the TTF file
I have a third party library that I'm attempting to incorporate into a simulation.
Attempting to better understand chaining on a hashtable. Seeking a simple table that hashes
I understand that doing what I'm attempting is very bad programming form (relying on
I am attempting to debug an Android application that uses native C code. I
When attempting to understand how a SQL statement is executing, it is sometimes recommended
Still struggling to understand what best practices are with respect to macros. I'm attempting
Attempting to print out a list of values from 2 different variables that are
Attempting to make a NSObject called 'Person' that will hold the login details for
I am attempting to visualize / understand my Rails application hierarchy on each page

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.