0

Having some trouble figuring out this error message as I write the Binary Search function. Any advice greatly appreciated!

bool binarySearch(int key, int array[], int min, int max);

bool search(int value, int values[], int n)
{
    // runs the search through binarySearch
    binarySearch(value, values[], 0, n);
}

bool binarySearch(int key, int array[], int min, int max)
{
    if (max < min)
        return false;
    else
    {
        int midpoint = ((max - min) / 2) + min;

        if (array[midpoint] < key)
            binarySearch(key, array[], midpoint + 1, max);

        else if (array[midpoint] > key)
            binarySearch(key, array[], min, midpoint -1);

        else
            return true;
     }
}

errors

helpers.c:23:32: error: expected expression
binarySearch(value, values[], 0, n);
                           ^
helpers.c:36:37: error: expected expression
        binarySearch(key, array[], midpoint + 1, max);
                                ^
helpers.c:39:37: error: expected expression
        binarySearch(key, array[], min, midpoint -1);
                                ^

3 errors generated.

2 Answers 2

4

It looks like the three errors are on lines that call binarySearch(...). The issue is with the array parameter in the call.

When you define the function (when you write the prototype) you need to show that something is an array with the form type name[], like you did with int array[]. However, when you call the function, you want to pass the entire array. You do this without the [] brackets. So, your calls should look like this:

    binarySearch(key, array, min, midpoint -1);

If you were to include the brackets, you would need something inside them. For example, if you had an array of arrays, you would select one element from that array that points to another array. But that's an example for another time. ;-)

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

2
  • well, technically he'll not be "passing the entire array". just a pointer to it. ;)
    – kzidane
    Commented Jul 1, 2015 at 23:17
  • true, but I didn't want to complicate it any further. Besides, didn't we have this debate once before? And weren't we each saying exactly the opposite, you saying that arrays are passed directly, while I said by reference/address? ;-)
    – Cliff B
    Commented Jul 3, 2015 at 1:14
0

Your error comes from the way you call your function. Say we have the following function:

bool foo(int myArray[])
{
    // do your stuff here
}

You will call this function like that:

int tmpArray = {1, 2, 3} ;
foo(tmpArray); // and not foo(tmpArray[]);
2
  • in C, arrays are surrounded with curly braces not square brackets.
    – kzidane
    Commented Jul 1, 2015 at 23:18
  • Oops. After so much time with js you forget the most basic things. Thanks @Kareem
    – ChrisG
    Commented Jul 2, 2015 at 0:11

You must log in to answer this question.

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