ProgrammingModerate
A sorted array of struct items is searched with bsearch for a key that is not in the array. What does bsearch return?
- A. A pointer to the first element
- B. A pointer one past the last element
- C. A pointer to the element just below the key
- D. NULL
Correct answer: D. NULL
bsearch returns NULL when no element compares equal to the key. The caller must test the result before dereferencing it.
More C Programming questions
- An array struct P ps[] of pointers to records is to be sorted by an int field k with qsort. Which comparator is correct?
- For struct R { char c; short s; int i; } with natural alignment (char 1, short 2, int 4), what is sizeof(struct R)?
- Assume natural alignment with 4-byte int. What is sizeof(struct R[5]) where struct R is the eight-byte type from the previous record?
- struct Q { char name[6]; double score; char tag; }; with 8-byte double and natural alignment. What is sizeof(struct Q)?
- Assume natural alignment: char 1, double 8. What is sizeof(struct T) for struct T { char a; double d; char b[3]; }?
- Binary search with lo = 0, hi = n - 1 on n = 1000 sorted records, looking for a key not in the array, performs how many probes at most?