Determining index from bsearch and lfind?

孤街浪徒 提交于 2019-12-11 08:26:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!