How would you use bsearch() function to search a name stored in array of pointers to string?
Answer / Rajendra Kumar Nishad
To use the bsearch() function, you need to provide a comparison function similar to qsort(). Here's an example:
```cn
#include <stdio.h>n
#include <stdlib.h>n
n
typedef struct {n char *name;n } Person;n
n
int compareNames(const void *a, const void *b, const void *c)n{n return strcmp(((Person *) a)->name,n ((Person *) b)->name) - (*(const char **) c);n}n
n
int main() {n Person people[] = {"Alice", "Bob", "Charlie", "Dave"};n const Person *person = bsearch(&"Eve", people, sizeof(people) / sizeof(people[0]), sizeof(Person), compareNames);n if (person != NULL) {n printf("%s found.
", person->name);n } else {n printf("Not found.
");n }n}n ```
| Is This Answer Correct ? | 0 Yes | 0 No |
What is stack push?
Which is better hashset or treeset?
Why do we use linked lists?
What do you mean by linear probing?
What is a node in it?
Define depth and height of a node?
What do you mean by tree edge?
Which is better merge or quick sort?
Write is a binary search tree? Write an algorithm and tell complexity?
1 Answers ITC Indian Tobacco Company,
What are the advantages of array?
Differentiate between set and map.
State the different types of linked lists?