views:

85

answers:

2

I have the same professor: I have read the forum:

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...

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.

+2  A: 

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];?

Justin Smith
Let me edit above..
MRP
I forgot to add in bzero(mylist, sizeof(LLIST *)*x);
MRP
+1  A: 

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);  
}
insipid
That should fix it? Does my function calls look right?
MRP
bzero is surely causing a segfault because its zeroing an undefined location in memory
insipid
Ok I will give it a try.
MRP
is it implemented like bzero? I'm not familiar with much C code
MRP
ugh LLIST **mylist = (LLIST *)malloc(sizeof(LLIST *)*x).. sorry its late
insipid
I need though the array factor..
MRP
ha okay thanks!
MRP
you can still use it like an array mylist[1], mylist[2]..
insipid
I'm trying it now
MRP
I still get the Seg fault.. How do you use the debugger?
MRP
Pretty much if I remove the list_map() function it works... Well It doesn't have the Seg Error
MRP
Your sure you got output after that call to bzero?? If so then I am quite suprised!
insipid
I changed it to what you said (taking out the bzero)... I took out the list_map() to see if I get any seg errors and I didnt ... but when I ran it with the list_map().. I get the same errors..
MRP
wait...........
MRP
Yeah your right...
MRP
it is also possible you were not supposed to have an array of pointers which is what LLIST *mylist[x]; implies.. you would want LLIST mylist[] isntead of LLIST *mylist[]
insipid
no He suggested LLIST *mylist[]
MRP
I tried just about anything even changing * in front of the functions
MRP
I was hopping it was something simple as LLIST **mylist = (LLIST *)malloc(sizeof(LLIST *)*x).
MRP
Do you need to see more code?
MRP
myprogram.c:16: warning: initialization from incompatible pointer typemyprogram.c:42: warning: passing argument 2 of \u2018list_map\u2019 from incompatible pointer type
MRP
thats some other stuff i get dont know if that will help you
MRP
LLIST **mylist = (LLIST *)malloc(sizeof(LLIST *)*lines); is line 16
MRP
should have been two *'s im new to stack overflow, it chomped it.. see my answer edit.. also this has turned into a discussion which I believe is discouraged feel free to email me
insipid
I tried what you posted.. I get the error error: array size missing in \u2018mylist\u2019
MRP
why is it discouraged...? I know I am frusterated
MRP
thats becuase im dumb.. fixed
insipid
warning: passing argument 2 of \u2018list_map\u2019 from incompatible pointer type ... Thats at the function call
MRP
that warning is because your function pointer expects to have a void * argument and you gave it LLIST **
insipid
no your not dumb... I am new to this form and C programming...
MRP
still doesnt work... thought that might be a problem.
MRP
You have been a great help.. I will email you my code.. Thank you for your time.
MRP