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

I

Ingo Brueckl

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
 
G

Grumble

Ingo said:
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
 
M

Martin Dickopp

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
 
B

Ben Pfaff

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)[] = { ... };
 
K

Keith Thompson

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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top