Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to declare an array of sort functions for qsort()?

Reply
Thread Tools

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

 
 
Ingo Brueckl
Guest
Posts: n/a
 
      12-18-2003
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
 
Reply With Quote
 
 
 
 
Grumble
Guest
Posts: n/a
 
      12-18-2003
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

 
Reply With Quote
 
 
 
 
Martin Dickopp
Guest
Posts: n/a
 
      12-18-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
Ingo Brueckl
Guest
Posts: n/a
 
      12-18-2003
> 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
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      12-18-2003
(E-Mail Removed) (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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-19-2003
(E-Mail Removed) (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) (E-Mail Removed) <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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Merge Sort in C - array output is same as input after sort routine completes rkk C Programming 9 09-24-2006 08:30 PM
Function pointers, variable argument functions calling other variable-argument functions (sort of) S?ren Gammelmark C Programming 1 01-07-2005 09:41 PM
Array sort function sorts on chars not numbers ... help ! how to sort numbers GIMME Javascript 5 07-26-2004 01:28 AM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments