Thread: An interesting problem of qsort()

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    An interesting problem of qsort()

    As you know, the qsort has the signature:
    void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );

    The following code is from http://www.cplusplus.com/reference/c...lib/qsort.html
    Is it wrong? Seems
    Code:
    qsort (values, 6, sizeof(int), compare);
    should be
    Code:
    qsort (values, 6, sizeof(int), &compare);
    Code:
    /* qsort example */
    #include <stdio.h>
    #include <stdlib.h>
    
    int values[] = { 40, 10, 100, 90, 20, 25 };
    
    int compare (const void * a, const void * b)
    {
      return ( *(int*)a - *(int*)b );
    }
    
    int main ()
    {
      int n;
      qsort (values, 6, sizeof(int), compare);
      for (n=0; n<6; n++)
         printf ("%d ",values[n]);
      return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is not wrong. When passed as an argument, a function is converted to a function pointer.
    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 hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    In the above context, "compare" is the address of the function, you do not therefore need to prefix that with '&'.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Is this C++?
    If so, you should consider using std::sort() instead of the old qsort() function.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by meili100 View Post
    Code:
    int compare (const void * a, const void * b)
    {
      return ( *(int*)a - *(int*)b );
    }
    Sidebar: the comparison function has issues with integer over/underflow and is not a good example of well-defined behavior.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    the name of a function, like the name of an array, can be assigned to a pointer of the same type. No need for the address-of operator.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    . . . but unlike arrays, you can use either way with function pointers. And there are two ways to call function pointers as well.
    Code:
    #include <iostream>
    
    void func() {
        std::cout << "func()\n";
    }
    
    int main() {
        void (*f)();
    
        f = func;
        f = &func;
    
        f();
        (*f)();
    
        return 0;
    }
    The reason for this is that some people thought one way was better, and others the other way. Since both arguments made sense, ANSI incorporated both into C. And C++ got it from there.

    (All of these arguments are from memory, they may be wrong . . . .)

    The argument for func: when you call a function you use parentheses, so leaving off the parentheses is like leaving off the [] array index operators: you should get a pointer. A similar argument applies for func().

    The argument for &func: func is a single object. With arrays, it's true, you can use "array" to get a pointer; but all single objects, be they structures or numbers, you need to use an & to get the address. A similar argument applies for (*func)().

    For more detailed information, see: http://www.newty.de/fpt/index.html
    (I know it says &func is better than func, but I don't think that's so.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. qsort problem
    By studentc in forum C Programming
    Replies: 3
    Last Post: 05-24-2004, 02:09 PM
  2. Interesting Problem with IOCP and AcceptEx() :: Winsock
    By kuphryn in forum Networking/Device Communication
    Replies: 0
    Last Post: 10-07-2003, 09:16 PM
  3. Interesting number theory problem
    By Zach L. in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-20-2003, 07:45 AM
  4. Interesting virtual function problem.
    By Sebastiani in forum C++ Programming
    Replies: 12
    Last Post: 09-02-2003, 10:08 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM