Thread: Compare function for qsort

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    29

    Compare function for qsort

    While looking for information on how to use the qsort function here on the board, I came across this:
    Code:
    int compare(void const *name1, void const *name2)
    {
        char *const *x = (char *const *)name1;
        char *const *y = (char *const *)name2;
    
        return strcmp(*x, *y);
    }
    I know the local variables are there just to make the function more readable but what I don't understand is why x and y are declared as char *const *. I know strcmp takes two const *char as its parameters but when I tried to declare x and y as const *char, the program won't sort the lines correctly. Thanks.

  2. #2
    Registered User white's Avatar
    Join Date
    Nov 2004
    Posts
    39
    depends on what you are comparing for doubles I use the following::
    Code:
    int dcmp(const void* a, const void* b)
    {
    	const D *x = a, *y = b;
    	if ( *x > *y )
    	{return 1;}
    	if ( *x < *y )
    	{return -1;}
    	return 0;
    }
    ----------------

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >but what I don't understand is why x and y are declared as char *const *
    I would've thought you could've just done this.
    Code:
    int compare(void const *name1, void const *name2)
    {
        const char *x = (const char *)name1;
        const char *y = (const char *)name2;
    
        return strcmp(x, y);
    }
    I have no idea why they used a double pointer.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @white: You don't need to return -1, 0, 1 for use in qsort. You can return <0, 0, >0. This means you can simplify your code to:
    return (*x - *y);
    You can go further, and avoid the use of local variables completely.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    29
    swoopy, it won't work if I change the code to what you suggested

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    post your code then. (a compilable sample, showing the problem)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    29
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int compare(const void *name1, const void *name2)
    {
        char *const *x = (char *const *)name1;
        char *const *y = (char *const *)name2;
    
        return strcmp(*x, *y);
    }
    
    int main(void)
    {
        char *names[] = {"Meg", "John", "Susan", "Charlotte", "Mario"};
        int nelements = sizeof(names) / sizeof(*names);
    
        char **p = names;
    
        qsort(names, nelements, sizeof *names, compare);
    
        while (nelements-- > 0)
            printf("%s\n", *p++);
    
        return 0;
    }
    This works perfectly. Now try changing the declarations of x and y in function compare to anything but that...

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Sorry, didn't read your post properly first time! Here's your answer:
    qsort passes a pointer to the array element, not a copy of the array element. Your array elements are type char*, therefore a pointer to that would be char**.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > return (*x - *y);
    Except you risk arithmetic underflow and the probable wrong answer which results.
    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. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM