-2

Let's suppose we have a structure:

     struct product 
 {
    char name[30]; 
    float price;
 };

I want to sort it using qsort first by price, and if the prices are equal, by name. How I thought of writing the compare function:

    int compare(const void *a, const void *b )
{
    int comp =  a.price - b.price;

    if (comp < 0 )
    return 1
    if (comp > 0 )
        return 0;


    comp = strcmp(a.name, b.name);

   if ( comp  < 0 )
       return 1;
   else
   if ( comp > 0 ) 
       return 0;

}

Since I have only used the usual compare function for qsort, I don't know how to go about this. I think that I'm accessing the fields incorrectly, based on the errors given, so could you please point out my mistakes writing the compare function?

5
  • 1
    this is C, not C++... and you have several syntax errors in your code, I suggest you to read it carefully and look for these by yourself, or you will never progress Commented May 19, 2014 at 7:29
  • If you're using C++, don't use qsort, use std::sort instead (and use std::string). If you're using C, please edit your question and check other examples of qsort usage. Also turn on your compiler warnings to the max once you've fixed the type problems. Commented May 19, 2014 at 7:36
  • the mysterious "errors given" Commented May 19, 2014 at 7:40
  • Okay, I managed to use std::sort successfully. Also, the errors were related to accessing the structure's fields: a.price gave me a " 'a' does not have class type" error, if I well remember. I was not aware that std::sort is the way to go in C++, since I only recently switched from C. Thank you for your replies. Commented May 19, 2014 at 7:59
  • possible duplicate of How to use qsort for string in C++ Commented May 19, 2014 at 12:10

1 Answer 1

2

Your code as written has several syntax errors, and also your comparison function doesn't do quite what you want. Quoting from the manpage for qsort:

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respec‐ tively less than, equal to, or greater than the second. If two mem‐ bers compare as equal, their order in the sorted array is undefined.

Consider the following code:

#include <stdlib.h>
#include <string.h>

struct product {
    char name[30]; 
    float price;
};

int compare(const void *a, const void *b) {

  const struct product *x = a;   // void* can be assigned to any other pointer type
  const struct product *y = b;

  int comp =  x->price - y->price;

  if (comp < 0)
    return -1;

  if (comp > 0)
    return 1;

  comp = strcmp(x->name, y->name);

  return comp;
}

If you want to reverse the sort order, negate comp at the appropriate place.

As others have mentioned, this is C code, and is not idiomatic C++.

Sign up to request clarification or add additional context in comments.

2 Comments

Note: void* CANNOT be assigned to any other pointer in C++.
Question is not tagged C++, so the comment above this one is completely off-topic, and your edit to the end of your post is superfluous. (As so many on this site point out, C and C++ are two different languages.

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.