我正在用 C 编写一个向量。如果已排序,则 CVectorSearch 函数使用 bsearch,如果未排序,则使用 lfind。为什么我在调用 lfind 时会收到警告“赋值从整数中生成指针而不进行强制转换”?即使在使用 lfind 时,它似乎也能正常工作。
typedef struct
{
void *elements;
int logicalLength;
int allocatedLength;
int elementSize;
} CVector;
typedef void (*CVectorFreeElemFn)(void *elemAddr);
int CVectorSearch(const CVector *v, const void *key,
CVectorCmpElemFn comparefn,
int startIndex, bool isSorted)
{
void * found;
int elemSize = v->elementSize;
int length = v->logicalLength;
void *startAddress = (char*)v->elements + startIndex*elemSize;
if(isSorted)
found = bsearch(key, startAddress, length, elemSize, comparefn);
else
found = lfind(key, startAddress, &length, elemSize, comparefn);
if(found)
return ((char*)found - (char*)v->elements) / elemSize;
else
return -1;
}
编辑: 现在我已经包含了 search.h 我得到了:
warning: passing argument 3 of 'lfind' from incompatible pointer type
不过,该程序仍然可以正常工作。