I’m getting a segmentation fault on the lfind call with this code. CVector is a struct with an array called elems. I know the CVectorCreate and CVectorAppend functions work. The first block is the test code, which is provided as part of the class and can’t be changed, and the second is the function call that I wrote. Can someone help me identify my issue? Thanks!
char *jumbled = "xatmpdvyhglzjrknicoqsbuewf";
CVector *cv = CVectorCreate(sizeof(char), 4, NULL);
for (int i = 0; i < strlen(jumbled); i++)
CVectorAppend(cv, &jumbled[i]);
printf("\nDoing linear searches on unsorted cvector.\n");
char ch = '*';
Verify(0, CVectorSearch(cv, &jumbled[0], CmpCharElem, 0, false), "Linear search");
int CVectorSearch(const CVector *cv, const void *key, CVectorCmpElemFn cmpfn, int startIndex, bool isSorted)
{
assert(startIndex >= 0 && startIndex <= cv->logicalLength);
void *found = NULL;
if (isSorted == true) {
found = bsearch(key, (char *)(cv->elems) + (startIndex * cv->elemSize),
cv->logicalLength, cv->elemSize, cmpfn);
} else {
found = lfind(key, (char *)(cv->elems) + (startIndex * cv->elemSize), cv->logicalLength, cv->elemSize, cmpfn);
}
Unlike
bsearch, the third argument tolfindis a pointer.