3

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

2
  • is this homework? It looks like the sort of question I might have set when I worked in education. Commented Mar 23, 2012 at 21:11
  • What does your code look like which tries to sort one of the arrays? Have you started with an array of Emp data[empSize]; to make it a bit easier to get your head around it? I'd start with an array ofthe structs, get that working, then use the array of pointers. Then write the scond comparison function, and use the secoond array of pointers. Commented Mar 23, 2012 at 21:14

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

Comments

1

An example for sorting on names:

#include <stdio.h>
#include <stdlib.h>
#define MAX_NAME 20
#define NUM_MONTHS 12

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

int compareName(const void * a, const void * b)
{
  return (strcmp(((Emp*)a)->name, ((Emp*)b)->name));
}

int main()
{
  Emp *data;
  int empSize = 100;
  qsort(data, empSize, sizeof(Emp), compareName);
  // qsort(data, empSize, sizeof(Emp), compareSales);
  return 0;
}

3 Comments

Why are you using sizeof(int) as the third argument to qsort()?
@PlatinumAzure, size: Size in bytes of each element in the array
Technically, in your example the elements are Emps (not even pointers to them), but in the OP's example they are Emp *s. Those are not guaranteed to be the same size as sizeof(int) especially on a 64-bit system. Also, it's much safer to just use sizeof(*data) anyway. That way it's more generic and future-proof.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.