问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/16211667/determining-index-from-bsearch-and-lfind