C - how to convert a pointer in an array to an index?

你。 提交于 2019-12-22 03:52:49

问题


In the many search functions of C (bsearch comes to mind) if a result is found, a pointer to the spot in the array is returned. How can I convert this pointer to the index in the array that was searched (using pointer arithmetic, i assume).


回答1:


ptrdiff_t index = pointer_found - array_name;


来源:https://stackoverflow.com/questions/2711653/c-how-to-convert-a-pointer-in-an-array-to-an-index

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