A1fredPennyworth

Untitled

Jan 2nd, 2023
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3.  
  4. typedef struct BSearchResult {
  5.     size_t low;
  6.     size_t high;
  7.     int result;
  8. } BSearchResult;
  9.  
  10. typedef struct BSearchResult BSearchResult;
  11.  
  12. BSearchResult bsearch2(const void *key, const void *base, size_t nmemb, size_t size,
  13.          int (*compar)(const void *p1, const void *p2, void *user),
  14.          void *user);
  15.  
  16. int compar(const void *lhs, const void *rhs, void *) {
  17.     return *(int*)lhs - *(int*)rhs;
  18. }
  19.  
  20. int main() {
  21.     int arr[5] = {1, 1, 1, 3, 4};
  22.     int key = 2;
  23.     BSearchResult result = bsearch2(&key, arr, 5, 4, compar, NULL);
  24.     printf("%ld %ld %d\n", result.low, result.high, result.result);
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment