Help with segmentation fault in function?

Up vote 0 down vote favorite share g+ share fb share tw.

I've been trying to solve this bsearch homework problem for awhile now. I try using my code to first search for one entry like so: int Compare(const void *a, const void *b); void SortStudents(char *studentList, size_t studentCount) { qsort(studentList, studentCount, sizeof(studentList0), Compare); } int Compare(const void *a, const void *b) { return (strcmp(*(char **)a, *(char **)b)); } char *SearchList(char *key, char *list, size_t num) { char **value = bsearch(&key, list, num, sizeof(list0), Compare); return (value == 0? 0 : *value); } /*Determines which registrants did not attend the first meeting by searching for registrants that are not in attendees set.

*/ void DisplayClassStatus( const char *registrants, size_t registrantCount, const char *attendees, size_t attendeeCount) { char *missedFirstMeeting = SearchList((char *)registrants0, (char **)attendees, attendeeCount); } My missedFirstMeeting seems to work in calling out a single value properly, but when I try to repeatedly call my SearchList function in a loop like so: for (int I = 0; I Thanks. C segmentation-fault qsort link|improve this question edited Feb 21 '10 at 20:45Juan2,2401018 asked Feb 21 '10 at 20:22Crystal2,353932 81% accept rate.

Remove the leading '*' of firstMeeting: missedFirstMeeting = SearchList((char *)registrantsi, (char **)attendees, attendeeCount).

Ok, so the problem is the following, you iterate over registrants but your for stops when it's proccessed attendeeCount items. And also, if missedFirstMeeting is a char*, do as tur1ng said, you need to remove the leading *. So just do this: for (int I = 0; I.

I thought I had to use the * to say that I want the value of missedFirstMeeting to be set to the return value of SearchList, but I guess that is incorrect. But in doing it this way with a for loop, does missedFirstMeeting only get a pointer to one element at a time? And if I did something like missedFirstmMeetingi, then it would be an array of values?

– Crystal Feb 21 '10 at 21:05 I edited my answer to show how you would do to store all the results in array. – Juan Feb 21 '10 at 21:20.

Ok, you need to put the return values functions into a variable. I write C++ for a living my boss would never accept code like this. This code is very difficult to read and even harder to debug.

The reason why is because you can setup watches for a variable. You can also see how the program is chaining as you step though the program line by line. A debugger will list all variables in your name space and their corresponding values.

I bet money that while you are rewriting this to be more readable, you'll figure out the problem your self.

There are (at least) two problems I see with your code. First, trivial but serious is that the for loop should compare I versus registrantCount not attendeeCount. Second Compare() should be written: int Compare(const void *a, const void *b) { return (strcmp((char *)a, *(char *)b)); } You simply need to cast the void pointers to char pointers.

Fixing these should fix your SegFault errors. Added: The dereferencing of missedFirstMeeting in the for loop is one of the two major problems. For (i = 0; I.

Compare is correct. Bsearch looks over a char** variable so when it wants to compare key to the i-th element, it will do Compare(key, haystack+i*size), thus passing on a char** not a char* in both arguments. – Juan Feb 21 '10 at 20:44 Compare isn't needed in the first place.

OP can just pass strcmp to qsort and bsearch. – qrdl Feb 21 '10 at 20:54 @Juan, you are correct, thank you. – mctylr Feb 21 '10 at 21:09.

The problem is that although computers are big blocks of mutable memory, that's not the best way to imagine them when programming them. You modeled the computer that way, and it didn't quite work out.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions