Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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).

like image 408
Tony Stark Avatar asked Apr 26 '10 07:04

Tony Stark


1 Answers

ptrdiff_t index = pointer_found - array_name;
like image 117
Khaled Alshaya Avatar answered Sep 25 '22 01:09

Khaled Alshaya