1

When attempting to implement binary search, I have run into problems when compiling. If you see any problems in sort, please point them out as well. Any help would be appreciated. Here are the errors:

clang -ggdb3 -O0 -std=c99 -Wall -Werror -o find find.c helpers.c -lcs50 -lm

helpers.c:20:6: error: conflicting types for 'bsearch'
bool bsearch(int key, int array[], int min, int max)
     ^
/usr/include/stdlib.h:755:14: note: previous declaration is here
extern void *bsearch (const void *__key, const void *__base,
         ^
helpers.c:34:52: error: too few arguments to function call, expected 5, have         4
        return bsearch(key, array, mid + 1, max);    
               ~~~~~~~                         ^
/usr/include/stdlib.h:755:1: note: 'bsearch' declared here

extern void *bsearch (const void *__key, const void *__base,

^
helpers.c:38:52: error: too few arguments to function call, expected 5, have    4
            return bsearch(key, array, min, mid - 1);
                   ~~~~~~~                         ^
/usr/include/stdlib.h:755:1: note: 'bsearch' declared here
extern void *bsearch (const void *__key, const void *__base,
^
 helpers.c:52:39: error: too few arguments to function call, expected 5,  have 4
   bsearch(value, values, values[0], n);     
   ~~~~~~~                            ^
/usr/include/stdlib.h:755:1: note: 'bsearch' declared here
extern void *bsearch (const void *__key, const void *__base,
^
4 errors generated.
make: *** [find] Error 1

Here is my code:

/**
 * helpers.c
 *
 * Computer Science 50
 * Problem Set 3
 *
 */

#include <cs50.h>
#include <math.h>

#include "helpers.h"

/**
 * Returns true if value is in array of n values, else false.
 */
 //Recursive Search

bool bsearch(int key, int array[], int min, int max)
{
    int mid1 = 0;

[mod: snipped code as not necessary to answer question.]

2

You have a naming conflict with the previously defined function bsearch() in stdlib. Try changing the name of your bsearch() function to something like binsearch() and recompile.

If this answers your question, please click the check mark to accept this and remove the question from the unanswered pool. Let's keep up on forum maintenance. ;-)

1
  • facepalm Ok, thanks!
    – mmoore
    Aug 17 '15 at 20:27
0

There is already a function predefined in C with the function name bsearch(). You can read more about it HERE.

Change your function name to anything else other than bsearch()

You must log in to answer this question.

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