我試圖調用快速排序使用結構「學生」 數組我比較它的屬性:比較中的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 –