ProgrammingHard

A bsearch on a struct array returns NULL for a key that is actually present. Which is the most likely cause?

  1. A. The key was passed by pointer
  2. B. The array has more than 10 elements
  3. C. The array is not sorted with the same comparator that bsearch uses
  4. D. The array was allocated with malloc

Correct answer: C. The array is not sorted with the same comparator that bsearch uses

bsearch assumes the array order matches its comparator. If the sort used a different order, the probes can miss an existing key.

More C Programming questions

Exams that ask this topic