调用 qsort() 函数
Calling the qsort() function
我正在编写这个需要在 table 上执行 qsort() 的程序。我弄完了
在互联网上进行了广泛的研究,但我仍然遗漏了一些东西。下边是
源代码的选定部分。
#define NOSLACK __attribute__((packed))
#define TABWIDTH 100 /* width of big table */
#define TABSIZE 100 /* number of entries in big table */
struct ELEMEN
{char flags; /* flags pertaining to this task */
short int tasknum; /* number of THIS task (excluding any step number) */
short int numpre; /* number of prereqs this task has */
short int numpost; /* number of postreqs this task has */
short int prereqs[0]; /* table of prereqs (omitted if numpre = 0) */
short int postreqs[0]; /* table of postreqs (omitted if numpost = 0) */
char fragment[TABWIDTH /* fragment of the descrip- */
- sizeof(char) /* tion; as much as will fit */
- sizeof(short int) * 3];} NOSLACK;
/* the lengths of all the above fields should total to TABWIDTH */
struct ELEMEN bigtable[TABSIZE];
short int e35(const void*, const void*);
short int main(int argc, char* argv[])
qsort(g.bigtable, numelem, TABWIDTH, /* sort table using e35() */
e35); // <--- PROBLEM HERE
short int e35(const void* elem1, const void* elem2) /* sort tasks */
{return(memcmp( /* into se- */
(short int*)&((struct ELEMEN*)elem1)-> tasknum, /* quence by */
(short int*)&((struct ELEMEN*)elem2)-> tasknum, /* task number */
sizeof(short)
));
问题出在对 qsort() 的调用中。如果最后一个参数只是 e35,我会收到警告消息:警告:从不兼容的指针类型 [-Wincompatible-pointer-types]
传递‘qsort’的参数 4
如果我将 e35 更改为 (e35)(const void*)(const void*)),我得到:错误:预期
“const”之前的表达式和错误:函数“e35”的参数太少
如果我将其更改为错误:e35(asterisk)(const void*)(const void*)) 我得到:错误:')' 标记错误之前的预期表达式:函数'e35' 的参数太少并且错误:'const'
之前的预期表达式
我错过了什么?我相信 gcc 不知道 e35 是一个函数,而不是一个变量或数组。
以前用过qsort(),但是在gcc下还是第一次。以前,我在 TurboC 下使用它,我只需要函数的名称,e35。
我在 Debian Linux 下使用 gcc 进行编译。 qsort() 的目的是我以后反复搜索 table,并希望能够更快地进行搜索。
(附带问题,只是为了好玩:为什么我将比较例程称为 e35?它来自哪里,有什么意义?也许我应该把这个附带问题放在困惑或逆向计算上.)
使用 int
return。删除不必要的演员表。比较为 short
。避免减法溢出。
int e35(const void* elem1, const void* elem2) {
const struct ELEMEN* t1 = elem1;
const struct ELEMEN* t2 = elem2;
return (t1->tasknum > t2->tasknum) - (t1->tasknum < t2->tasknum);
}
我正在编写这个需要在 table 上执行 qsort() 的程序。我弄完了 在互联网上进行了广泛的研究,但我仍然遗漏了一些东西。下边是 源代码的选定部分。
#define NOSLACK __attribute__((packed))
#define TABWIDTH 100 /* width of big table */
#define TABSIZE 100 /* number of entries in big table */
struct ELEMEN
{char flags; /* flags pertaining to this task */
short int tasknum; /* number of THIS task (excluding any step number) */
short int numpre; /* number of prereqs this task has */
short int numpost; /* number of postreqs this task has */
short int prereqs[0]; /* table of prereqs (omitted if numpre = 0) */
short int postreqs[0]; /* table of postreqs (omitted if numpost = 0) */
char fragment[TABWIDTH /* fragment of the descrip- */
- sizeof(char) /* tion; as much as will fit */
- sizeof(short int) * 3];} NOSLACK;
/* the lengths of all the above fields should total to TABWIDTH */
struct ELEMEN bigtable[TABSIZE];
short int e35(const void*, const void*);
short int main(int argc, char* argv[])
qsort(g.bigtable, numelem, TABWIDTH, /* sort table using e35() */
e35); // <--- PROBLEM HERE
short int e35(const void* elem1, const void* elem2) /* sort tasks */
{return(memcmp( /* into se- */
(short int*)&((struct ELEMEN*)elem1)-> tasknum, /* quence by */
(short int*)&((struct ELEMEN*)elem2)-> tasknum, /* task number */
sizeof(short)
));
问题出在对 qsort() 的调用中。如果最后一个参数只是 e35,我会收到警告消息:警告:从不兼容的指针类型 [-Wincompatible-pointer-types]
传递‘qsort’的参数 4如果我将 e35 更改为 (e35)(const void*)(const void*)),我得到:错误:预期 “const”之前的表达式和错误:函数“e35”的参数太少
如果我将其更改为错误:e35(asterisk)(const void*)(const void*)) 我得到:错误:')' 标记错误之前的预期表达式:函数'e35' 的参数太少并且错误:'const'
之前的预期表达式我错过了什么?我相信 gcc 不知道 e35 是一个函数,而不是一个变量或数组。
以前用过qsort(),但是在gcc下还是第一次。以前,我在 TurboC 下使用它,我只需要函数的名称,e35。
我在 Debian Linux 下使用 gcc 进行编译。 qsort() 的目的是我以后反复搜索 table,并希望能够更快地进行搜索。
(附带问题,只是为了好玩:为什么我将比较例程称为 e35?它来自哪里,有什么意义?也许我应该把这个附带问题放在困惑或逆向计算上.)
使用 int
return。删除不必要的演员表。比较为 short
。避免减法溢出。
int e35(const void* elem1, const void* elem2) {
const struct ELEMEN* t1 = elem1;
const struct ELEMEN* t2 = elem2;
return (t1->tasknum > t2->tasknum) - (t1->tasknum < t2->tasknum);
}