1

I'm trying to get the index of the element in the array after lfind and bsearch return the pointer to the element that it found. I have this so far:

(char *) (found - cv->baseAddress); 

where found is the address of what the functions found, and the base address is the address of element 0. However, the compiler gives me this error:

cvector.c:150:28: warning: pointer of type ‘void *’ used in subtraction cvector.c:150:4: warning: return makes integer from pointer without a cast

What do I do?

alk
  • 69,737
  • 10
  • 105
  • 255
girlrockingguna
  • 291
  • 1
  • 3
  • 13

1 Answers1

2

You have to typecast the returned pointer to the correct type. Then you also need to divide the offset you get with the size of the objects in the array to get the index.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • So would it look something like this: *(int *) (char *) found - cv->baseAddress / elemSize? @Joachim Pileborg – girlrockingguna Apr 25 '13 at 09:58
  • Can't really say without you showing us the call to bsearch() (and the type of your element array). – Medinoc Apr 25 '13 at 10:05
  • 2
    @girlrockingguna Almost, you need some extra parentheses there otherwise the division will be wrong: `((typeOfentries *) found - cv->baseAddress) / sizeof(typeOfEntries)`. – Some programmer dude Apr 25 '13 at 10:07