Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does bsearch return a void *?

void * bsearch ( const void * key,
                 const void * base,
                 size_t num,
                 size_t size,
                 int ( * comparator ) ( const void *, const void * ) );

If I pass in a const void * base, shouldn't bsearch also return a const void * result?

like image 350
fredoverflow Avatar asked Oct 29 '11 10:10

fredoverflow


1 Answers

When you search for something, it's a valid request that you be able to modify it after you found it. It would be too restrictive if the search function didn't allow you to do that. Of course such a modification could break a subsequent search, but that's a different matter.

The parameters are const as a promise that bsearch itself will not modify them, which is reasonable.

like image 114
Dabbler Avatar answered Nov 15 '22 18:11

Dabbler