If I want to write a sort function for a linked list that can use external functions for selecting between criteria (depending on whether the criteria is alpahabetic or numeric), what's the best way to pass this "selection field" option in? In other words, if I pass a node* in as the head, how can I also pass in an option to indicate which struct member should be used in the sort?
My idea was to use a pointer to the offset of the data within the struct. I'm hoping there won't be padding that causes a problem.
Code:
typedef struct _node {
char field1[64];
char field2[64];
int field3;
struct _node *next;
} node;
node *sortfunc (node *head, int offset, int datatype) {
node *one=head, *two=head->next;
char *one_a, *two_a;
int one_n, two_n;
if (datatype==0) {
one_i=&int(head+offset); // ???
result=compnumbers(one_i,two_i);
if (datatype==1) {
one_a=(char*)head+offset;
result=comparewords(one_a, two_a);