By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

Nov 14 '05 #2

P: n/a
ib@wupperonline.de (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
Nov 14 '05 #3

P: n/a
ib@wupperonline.de (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
Nov 14 '05 #4

P: n/a
> 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
Nov 14 '05 #5

P: n/a
ib@wupperonline.de (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) ks***@mib.org <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)
Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.