我试图调用快速排序使用结构“学生” 数组我比较它的属性:比较中的qsort没有要求[错误:比较之前预期的表达]
typedef struct
{
int ID; // 4 bytes = 164 [+ int]
char firstname[NAME_LENGTH]; // 1 bytes * length (80) = 160 [2 * NAME_LENGTH]
char lastname[NAME_LENGTH]; // 1 bytes * length (80)
} Student;
我的代码试图从函数中调用qsort 3次:按ID,然后名字,然后是姓氏对它进行排序。主函数处理将其他函数调用为读写操作。找到一个错误应该使我能够将它应用于其他功能,对吧?然而涉及排序功能是:
#ifdef TEST_SORTID
void StudentSortbyID(Student * stu, int numelem)
{
qsort(&(stu-> ID), numelem, sizeof(stu), compareInts);
}
#endif
#ifdef TEST_SORTFIRSTNAME
void StudentSortbyFirstName(Student * stu, int numelem)
{
qsort(&(stu-> firstname), numelem, sizeof(stu), compareStrings);
}
#endif
#ifdef TEST_SORTLASTNAME
void StudentSortbyLastName(Student * stu, int numelem)
{
qsort(&(stu-> lastname), numelem, sizeof(stu), compareStrings);
}
#endif
#ifdef TEST_COMPAREINTS
int compareInts(const void * argu1, const void * argu2)
{
const int * iptr1 = (const int *) argu1; //convert void to integer pointer
const int * iptr2 = (const int *) argu2;
int ival1 = * iptr1; //convert pointer to value
int ival2 = * iptr2;
if(ival1 < ival2) { return -1; } //return -1 if first value is less
if(ival1 > ival2) { return 1; } //return 1 if previous value is greater
if(ival1 == ival2) { return 0; } //return 0 if the adjacent values are equal
}
#endif
#ifdef TEST_COMPARESTRINGS
int compareStrings(const void * argu1, const void * argu1)
{
//String is an array of characters (string = char*) -> pointing to string
const char * const * sptr1 = (const char * *) argu1; //converting empty pointers to strings which point to characters [**]
const char * const * sptr2 = (const char * *) argu2;
const char * string1 = * sptr1; // a string is a character pointer
const char * string2 = * sptr2;
return strcmp(string1,string2);
}
#endif
当运行GCC是我得到的错误:
student.c:120: error: too few arguments to function ‘compareInts’
我想快速排序的比较不带参数?当我尝试放入数组的前两个元素时,它也会出错。有任何想法吗?
而且我们不应该理清错误信息可能涉及哪些行?对不起,我们不是一个debugginmg服务。 – Olaf
在调用qsort之前是否声明了比较函数? – dromtrund
对不起,错误发生在第4行@Olaf –