Thread: qsort

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    18

    qsort

    Hi all,

    i was reading in a book how to use the function qsort. the function needs a pointer to a compare function comp.
    sth like this was done :
    Code:
    char *data[MAX]; // memory was allocated and so on
    qsort(data, MAX, sizeof(data[0]), comp); // the call of qsort
    .
    .
    .
    int comp(const void *s1, const void *s2)
    {
        return (strcmp(*(char **)s1, *(char **)s2)); // what i don't understand
    }
    why in our exaplme we were using so many "*"
    i understand that (char **) is a typecasting, and the other one for dereferencing the pointer...

    so far when i wanted to compare two strings pointed to by ptr1 and ptr2 i used
    strcmp(ptr1, ptr2).

    i mean the comp function will be given pointers to strings, why not only :
    Code:
     return (strcmp((char *)s1, (char *)s2)); // typecasting from void* to char*

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,063
    It might be better to start with an array of int. As in, suppose that data was declared as:
    Code:
    int data[MAX];
    How would you then write the comparator function comp?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    18
    hmm, my comp function wil be given 2 void pointers.
    1. i need to typecast to int --> (int *)
    2. i need the value in the adress pointed to by the pointer (*)
    so :
    Code:
    int comp(const void *a, const void *b)
    {
        retrun(*(int *)a - *(int *)b)
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,408
    It's easier if you just declare some intermediate variables.
    Code:
    int compare ( const void *a, const void *b ) {
      const int *pa = a;
      const int *pb = b;
      // Note *pa - *pb may fail with numeric underflow
      if ( *pa < *pb ) return -1;
      if ( *pa > *pb ) return -1;
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    494
    Quote Originally Posted by Scatman View Post
    hmm, my comp function wil be given 2 void pointers.
    1. i need to typecast to int --> (int *)
    2. i need the value in the adress pointed to by the pointer (*)
    so :
    Code:
    int comp(const void *a, const void *b)
    {
        retrun(*(int *)a - *(int *)b)
    }
    Notice: You get the pointers to two elements from the original array. If you are dealing with an array containing pointers to arrays of chars (strings), these void pointers need to be cast to char **.
    Code:
    int comp( const void *a, const void *b )
    {
      const char **pa, **pb;
    
      pa = ( const char **)a;
      pb = ( const char **)b;
    
      return strcmp( *pa, *pb );
    }

  6. #6
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    91
    Quote Originally Posted by Salem View Post
    It's easier if you just declare some intermediate variables.
    Code:
    int compare ( const void *a, const void *b ) {
      const int *pa = a;
      const int *pb = b;
      // Note *pa - *pb may fail with numeric underflow
      if ( *pa < *pb ) return -1;
      if ( *pa > *pb ) return -1;
      return 0;
    }
    Both return -1 ?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,408
    Yep, well spotted!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. qsort
    By coder222 in forum C Programming
    Replies: 3
    Last Post: 09-18-2015, 12:06 PM
  2. Could use some help with qsort
    By Brolaf Broski in forum C Programming
    Replies: 8
    Last Post: 11-03-2013, 10:09 AM
  3. Help with qsort
    By muqing in forum C Programming
    Replies: 18
    Last Post: 04-12-2010, 02:16 AM
  4. Help here with qsort!
    By xxxrugby in forum C Programming
    Replies: 11
    Last Post: 02-09-2005, 08:52 AM
  5. qsort please help. Thanks
    By Ann Lim in forum C Programming
    Replies: 8
    Last Post: 12-12-2002, 02:20 AM

Tags for this Thread