参考“C – 帮助理解如何在函数内编写函数(list_map)”

我有同一位教授:我已经阅读了论坛:

如何在函数内编写函数(list_map)

理解函数的概念是非常有帮助的,但我不确定我是否正确使用它…

这是我的代码..如果我走在正确的轨道上,请告诉我……

假设我有一个包含10个链表的数组,其中链表包含整数

现在我想对列表调用list_map()进行排序; function

所以我的主要看起来像这样:

int x = 10; LLIST *mylist[x]; bzero(mylist, sizeof(LLIST *)*x); . . for(i=0; i< 10; i++) { //Segfalt list_map(mylist[i],sort); } 

我的list_map看起来像:

 void list_map( LLIST *list, void (*f)(void *) ) { printf("Sort"); f(list); } 

并排序:

 void sort(LLIST *n) { //Sort Code } 

我运行它时遇到的错误是Segmentation fault。

请原谅我没有代码,我知道我的排序function有效,我已经测试了它并打印出每个列表。 如果您需要更详细地了解某些内容,请告诉我我会提供的。

bzero将内存归零,不分配内存,使用malloc

 int x = 10; LLIST **mylist; mylist = (LLIST**)malloc(sizeof(LLIST *)*x); . . for(i=0; i< 10; i++) { //Segfalt list_map(mylist[i],sort); } void list_map( LLIST *list, void (*f)(void *) ) { printf("Sort"); f(list); } 

你在分配mylist吗? 基于你在这里所拥有的东西,任何访问mylist的东西都会导致段错误。 你确定应该是LLIST *mylist[x]; 而不是LLIST mylist[x];