Binary search implementation in C int bin_search(int* a, int key)
{
    int lo = 0;
    int hi = sizeof(a)/sizeof(int) - 1;

   while(lo &lt;=hi)
   {
        int mid = (lo + hi)/2;

        if(key &lt; a[mid]) { hi = mid - 1;}
        else if (key &gt; a[mid]) { lo = mid + 1;}
        else                    return mid;
    }

    return -1;
}


....

for(i = 0; i &lt; sz; ++i)
{
    printf("%d\n", bin_search(a, i));
}


I think that I have a problem with this, but I can't understand why:

int hi = sizeof(a)/sizeof(int) - 1;
sizeof(a) in this case is sizeof(int*) which is 4 or 8 on most machines. The most simple solution would be passing the length of the input array. Otherwise, your code looks nifty.