【发布时间】:2010-11-19 23:22:42
【问题描述】:
我正在用 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
不过,该程序仍在正常运行。
【问题讨论】: