谈到"Ç - 帮助了解如何将功能(list_map)QUOT中编写一个函数; [英] Referring to "C - Help understanding how to write a function within a function (list_map)"
问题描述
我也有同样的教授:我已阅读论坛:
I have the same professor: I have read the forum:
<一个href=\"http://stackoverflow.com/questions/2119558/c-help-understanding-how-to-write-a-function-within-a-function-listmap/2119913#2119913\">http://stackoverflow.com/questions/2119558/c-help-understanding-how-to-write-a-function-within-a-function-listmap/2119913#2119913
这是非常有用的理解函数的概念,但我不知道如果我使用它的权利...
It is very helpful understanding the concept of the function but I'm not sure if I am using it right...
下面是我的code ..让我知道如果我在正确的轨道上......
Here is my code.. Let me know if I am on the right track...
假设我有10链表中的链表持有整型数组
Assume that I have an array of 10 linked lists in which the linked lists holds ints
现在我想对列表进行排序调用list_map();功能
Now I would like to sort the list calling the list_map(); function
所以我的主要看起来像这样:
So my main looks something like this:
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如下:
my list_map looks like:
void list_map( LLIST *list, void (*f)(void *) )
{
printf("Sort");
f(list);
}
和排序:
void sort(LLIST *n) {
//Sort Code
}
我得到的错误是段错误,当我运行它。
The error I get is Segmentation fault when I run it.
请原谅缺乏code的,我知道我的排序功能的作品,我已经测试,它打印出每个列表。如果你需要看到更详细的让我知道,我会提供它。
Please excuse the lack of code, I know my sort function works, I have tested it already and it prints out each list. If you need to see something in more detail let me know I will provide it.
推荐答案
bzero零出内存不分配内存,使用malloc
bzero zeroes out memory it does not allocate memory, use 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);
}
这篇关于谈到&QUOT;Ç - 帮助了解如何将功能(list_map)QUOT中编写一个函数;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!