tags:

views:

85

answers:

2

I'm writing a vector in C. The CVectorSearch function uses bsearch if it's sorted, and lfind if it's unsorted. Why am I getting the warning "assignment makes pointer from integer without a cast" when I'm calling lfind? It seems to work fine even when lfind is being used.

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;
}

edit: Now that I've included search.h I'm getting:

warning: passing argument 3 of 'lfind' from incompatible pointer type

The program is still working correctly, though.

+3  A: 

Have you included <search.h> which defines lfind? If a function is called without a prototype, your compiler may assume it returns int.

Greg Hewgill
+1  A: 

The third argument to lfind() is a pointer to size_t not int as you are passing. The size_t type may be of a different size than int on some architectures (particularly x86-64) and it is also unsigned. You have to change the type of the length variable.

Juliano