How to declare an array of sort functions for qsort()?

Discussion in 'C Programming' started by Ingo Brueckl, Dec 18, 2003.

  1. Ingo Brueckl

    Ingo Brueckl Guest

    I need to declare a fixed array of (already defined and working) sort
    functions (sortfunc1, sortfunc2, sortfunc3) to pass one element of the
    array to qsort itself:

    int index;
    ??? func[] = {sortfunc1, sortfunc2, sortfunc3};

    index = /* calculated somehow */;

    qsort(what, what_elements, sizeof(what_element), func[index]);

    My problem: How must I declare the array correctly. (I won't use 'void *' as
    recent version of gcc don't accept it and I prefer correct declarations.)

    Ingo
     
    Ingo Brueckl, Dec 18, 2003
    #1
    1. Advertising

  2. Ingo Brueckl

    Grumble Guest

    Ingo Brueckl wrote:
    > I need to declare a fixed array of (already defined and working) sort
    > functions (sortfunc1, sortfunc2, sortfunc3) to pass one element of the
    > array to qsort itself:
    >
    > int index;
    > ??? func[] = {sortfunc1, sortfunc2, sortfunc3};
    >
    > index = /* calculated somehow */;
    >
    > qsort(what, what_elements, sizeof(what_element), func[index]);
    >
    > My problem: How must I declare the array correctly. (I won't use 'void *' as
    > recent version of gcc don't accept it and I prefer correct declarations.)


    Something along the lines of

    typedef int (*sort_fp)(const void *, const void *);


    int sortfunc1(const void *x, const void *y) { return 1; }
    int sortfunc2(const void *x, const void *y) { return 2; }
    int sortfunc3(const void *x, const void *y) { return 3; }

    sort_fp func[] = {sortfunc1, sortfunc2, sortfunc3};

    /* sizeof arr works only if arr is an array, not a pointer. */
    qsort(arr, sizeof arr, sizeof *arr, func[index]);

    If you don't like the typedef, you can read:
    http://shill.free.fr/vrac/cdecl.txt
     
    Grumble, Dec 18, 2003
    #2
    1. Advertising

  3. (Ingo Brueckl) writes:

    > I need to declare a fixed array of (already defined and working) sort
    > functions (sortfunc1, sortfunc2, sortfunc3) to pass one element of the
    > array to qsort itself:


    You can't do that. However, you can declare (and define) an array of
    pointers to functions.

    > int index;
    > ??? func[] = {sortfunc1, sortfunc2, sortfunc3};


    int (*const func []) (const void *, const void *)
    = {sortfunc1, sortfunc2, sortfunc3};

    Martin
     
    Martin Dickopp, Dec 18, 2003
    #3
  4. Ingo Brueckl

    Ingo Brueckl Guest

    > typedef int (*sort_fp)(const void *, const void *);
    > sort_fp func[] = {sortfunc1, sortfunc2, sortfunc3};


    > If you don't like the typedef, you can read:
    > http://shill.free.fr/vrac/cdecl.txt


    What a fun to read this! I've learned a lot about C declarations and made it
    now:

    int (*func[])(const void *, const void *) = {sortfunc1, sortfunc2,
    sortfunc3};

    Thanks a lot.

    Ingo
     
    Ingo Brueckl, Dec 18, 2003
    #4
  5. Ingo Brueckl

    Ben Pfaff Guest

    (Ingo Brueckl) writes:

    > I need to declare a fixed array of (already defined and working) sort
    > functions (sortfunc1, sortfunc2, sortfunc3) to pass one element of the
    > array to qsort itself:
    >
    > int index;
    > ??? func[] = {sortfunc1, sortfunc2, sortfunc3};
    >
    > index = /* calculated somehow */;
    >
    > qsort(what, what_elements, sizeof(what_element), func[index]);
    >
    > My problem: How must I declare the array correctly. (I won't use 'void *' as
    > recent version of gcc don't accept it and I prefer correct declarations.)


    typedef int comparison_func (const void *, const void *);
    typedef comparison_func (*func_ptr_array)[] = { ... };
    --
    "Some programming practices beg for errors;
    this one is like calling an 800 number
    and having errors delivered to your door."
    --Steve McConnell
     
    Ben Pfaff, Dec 18, 2003
    #5
  6. (Ingo Brueckl) writes:
    > I need to declare a fixed array of (already defined and working) sort
    > functions (sortfunc1, sortfunc2, sortfunc3) to pass one element of the
    > array to qsort itself:
    >
    > int index;
    > ??? func[] = {sortfunc1, sortfunc2, sortfunc3};
    >
    > index = /* calculated somehow */;
    >
    > qsort(what, what_elements, sizeof(what_element), func[index]);
    >
    > My problem: How must I declare the array correctly. (I won't use 'void *' as
    > recent version of gcc don't accept it and I prefer correct declarations.)


    It's a bit misleading to refer to these as "sort functions". They're
    more accurately referred to as "comparison functions". (qsort()
    itself is a sort function.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
    (Note new e-mail address)
     
    Keith Thompson, Dec 19, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Angus Comber
    Replies:
    7
    Views:
    1,163
    Richard Heathfield
    Feb 5, 2004
  2. PCHOME
    Replies:
    4
    Views:
    370
    Keith Thompson
    Jul 8, 2005
  3. Eddy C

    How would you use qsort to sort on a string

    Eddy C, Feb 9, 2006, in forum: C Programming
    Replies:
    31
    Views:
    1,789
    Jordan Abel
    Feb 13, 2006
  4. Replies:
    2
    Views:
    578
  5. Navin
    Replies:
    1
    Views:
    703
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page