I have this C
binary search program:
#include <stdio.h>
typedef int element_t;
int less(a, b) {
if (a < b)
return 1;
return 0;
}
element_t *binary_search(element_t x, element_t *a , int n) {
if (n > 0) {
int mid = n / 2;
if (less(a[mid], x))
return binary_search(x, a+mid+1 , n-mid-1);
if (less(x, a[mid]))
return binary_search(x, a, mid);
return a+mid;
}
return NULL;
}
int main(){
int a[5] = {1,2,3,4,5};
printf("%d %d", &a[2], binary_search(3, a, 5));
return 0;
}
I don't get this part a+mid+1
, it is supposed to pass an array there, but an address is instead, how is that? and array a
+
an integer is about address.
I read online and I can't see what is going on, because online it says a+1
is the address of value at index 1.