开发者

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:

How to write a function within a function (list_map)

It is very helpful understanding the concept of the function but I'm not s开发者_高级运维ure if I am using it right...

Here is my code.. Let me know if I am on the right track...

Assume that I have an array of 10 linked lists in which the linked lists holds ints

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);   
}

my list_map looks like:

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

and Sort:

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

The error I get is Segmentation fault when I run it.

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 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);  
}


Are you allocating mylist? Based on what you have here it looks like anything which accessed mylist would cause a segfault. Are you sure that should be LLIST *mylist[x]; and not LLIST mylist[x];?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜