我在使用此代碼訪問lfind時遇到了分段錯誤。 CVector是一個名爲elems的數組。我知道CVectorCreate和CVectorAppend函數可以工作。第一塊是測試代碼,它是作爲類的一部分提供的,不能改變,第二塊是我編寫的函數調用。有人可以幫助我確定我的問題嗎?謝謝!lfind的分割錯誤?
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);
}
'&混亂的[0]' - 只是通過在'jumbled',它已經是一個指針... – 2012-07-18 20:54:52