0

I am trying my hands on binary search. I get those pointer to integer errors :( Can sonmeone more experience give me a hand ?

bool search(int value, int values[], int n)
{
int first=0;
int last=n-1;
int middle=(first+last)/2;
while (values[first]!=values[last])
    middle=(first+last)/2;
    if (value==values[middle])
        {
        return true;
        }
    else if (value<values[middle])
        {
        last=(n/2)-1;    
        search(value,values[first],(n/2)-1);
        }
    else if (value>values[middle])
        {
        first=middle+1;   
        search(value,values[middle+1],(n/2)-1);
        }
if (values[first]==value)
    {
    return true;    
    }
else
    {
    return false;    
    }

the errors i get is one of those :

hello.c:35:22: error: incompatible integer to pointer conversion passing 'int' to parameter of type 'int *'; take the address with & [-Werror,-Wint-conversion] search(value,values[first],(n/2)-1);

0

The problem lies in the while loop. In your while loop condition, you might have wanted it to repeat till the first < last. However you have written values[first] != values[last].

1
  • I do not understand, my while loop should deal with the case that i have only 1 element to check left ? :( – hmmmbob Jul 8 '16 at 8:28
0

Look at the statement search(value,values[first],(n/2)-1); in your code. In the function definition bool search(int value, int values[], int n) you've declared second parameter as a pointer to array of integers. But in the function call you've put in array element of type integer as a second parameter values[first], where the function expect pointer to the array of integers. Of cause, compiler disagree with you. BTW, same mistake has been done in the next call: search(value,values[middle+1],(n/2)-1);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .