-
qsort using function ptr
I have two structs one which holds car information. The function I am writing requires to sort the list by an entered key. The list struct holds the function ptr to use. Can I use this pointer in qsort? I have tried
qsort(pcl->list,pcl->size,sizeof(pcl->list[i]),pcl->CompareFPtr);
but it gives the warning: parsing of arg 4 'qsort' from incompatible pointer type.
any ideas??
typedef struct
{
char *dynamicMake;
char *dynamicModel;
int year;
char rego[7];
} Car;
typedef struct List
{
Car list[LISTSIZE];
unsigned size;
int(*CompareFPtr)(Car*,Car*);
} List;
Any light shed appreciated.
-
Look at this:
Code:
#include <stdlib.h>
void qsort( void *base,
size_t num,
size_t width,
int (*compar) ( const void *,
const void *) );
The prototype differs to what you are using as the 4th arg:
>>int(*CompareFPtr)(Car*,Car*);
-
I have changed the function pointer to
[code]/*
int(*CompareFPtr)(const void*,const void*);
/*[code]
and still give parse error message is the correct syntax for a struct (pcl) holding a function pointer for the 4th arg of qsort
pcl->CompareFPtr ??
can i use a function pointer in arg 4 of qsort??
thanks
-
Thanks, fixed it
I had a structure with the function pointer stored ie
[code]*/
typedef struct List
{
Car list[LISTSIZE];
unsigned size;
int(*CompareFPtr)(const void*,const void*);
} List;
[code]
I have 4 different compare functions I store the relevant one to user in the struct function pointer. ie
[code]
int(*MakeCompare) (Car*, Car*),
int(*ModelCompare) (Car*, Car*),int(*YearCompare) (Car*, Car*),
int(*RegCompare) (Car*, Car*));
[code]/*
I have change my function pointer parameters to void and then type cast the relevant compare function when assigning it ie
[code]
pcl->CompareFPtr=(int (*)(const void*, const void*))MakeCompare;
[code]
i can now use qsort...
works fine and minimum changes.
-
nomes, have a read of this thread:
http://www.cprogramming.com/cboard/s...threadid=13473
It'll teach you about code tags. Alternatively, just look at my signature.
-
Code:
/*
printf("Sorry about that!! Have it sussed now!");
*/