0

From cppreference,

To perform the search, the function performs a series of calls to compar with key as first argument and elements of the array pointed to by base as second argument.

For a given array int arr[] = {1, 2, 3, 4, 5, 6, 7}, searching for the element 6 you need to pass a callback function to bsearch.Does my int compar (const void* pkey, const void* pelem); function needs to be able to return one of the following three cases:

  • return < 0
  • return > 0
  • return 0

or it suffice to implement the compar function just for equality (e.g return 0 when searched value equals current element)?

Cătălina Sîrbu
  • 1,253
  • 9
  • 30
  • I also removed the ambigous thing related to qsort. I wanted to refer to qsort as it's cb function needs to implement a return situation for all three cases (grater, less or equal to zero). Does the bsearch's cb function also needs to implement the three cases? Or just the equality counts – Cătălina Sîrbu Jan 01 '21 at 18:12
  • 2
    yes *bsearch* requires a function returning a negative value or 0 or a positive value – bruno Jan 01 '21 at 18:13
  • 2
    It needs to distinguish between the three cases - otherwise, how would the algorithm know which direction to move in the array? – Bob Jarvis - Слава Україні Jan 01 '21 at 18:13
  • @BobJarvis-ReinstateMonica, thanks! This is what I was missing, So the cb function is the one that "helps" moving back and forth in the array, and it needs to implement all three cases. – Cătălina Sîrbu Jan 01 '21 at 18:16

1 Answers1

2

Your comparison function must return the correct return value for all possible inputs. Furthermore, the vector must be sorted consistently wirh the comparison function: if the value a comes before b in the vector, then compar(&a, &b) must be less than or equal to 0. bsearch doesn't check that, but if it's not the case, bsearch will probably return the wrong result. Or worse.

Anyway, it's not possible to implement a function which only sometimes returns a value, if that's what you meant. Unless a function's return value is not used, the function must return something. C does not enforce this requirement, but if you ignore it, your program has Undefined Behaviour, which means that bad things will happen.

rici
  • 234,347
  • 28
  • 237
  • 341
  • The C standard does not require a function with a non-void return type to return a value. Program control may flow to the closing brace. The behavior is undefined only if there is an attempt to use the value of the function call. – Eric Postpischil Jan 01 '21 at 18:27
  • 1
    @eric: strictly speaking true but it obviously doesn't apply in this case since the return value of the `compar` function is always used. I did think about trying to be more pedantically correct but I couldn't think of a wording which would not be confusing for a novice. I'm open to suggestions. – rici Jan 01 '21 at 18:44
  • 1
    I do not think OP was contemplating a function that did not return a value in some cases, just a function that returned 0 if the inputs were equal in the ordering and something else if they were not (indiscriminately, so without regard to which was before the other). But, if they were, we can simply say that `bsearch`, rather than the C standard, requires the function to return a value in every case. – Eric Postpischil Jan 01 '21 at 19:01
  • So, basically as BobJarvis said, the other two situations (excluding the equality) are used in order for the binary search algorithm to move in the array – Cătălina Sîrbu Jan 01 '21 at 22:56
  • @Cătălina: yes, exactly. The search stops when it reaches an element for which compar() returns 0. – rici Jan 01 '21 at 23:09
  • Thanks this was the thing I could not understand, why I need to pass a function which better works as a sorting one :) – Cătălina Sîrbu Jan 01 '21 at 23:31