Thread: Using bsearch to search for a string in an array of strings.

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    10

    Using bsearch to search for a string in an array of strings.

    I have an array of strings called tab.
    I want to search for an array in it with the function bsearch() provided by <stdlib.h>.
    But I think I am doing something wrong, since it cannot find "delta".
    Is my code wrong?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define LEN(x) (sizeof (x) / sizeof (x[0]))
    
    char *tab[] = {
        "alfa",
        "beta",
        "delta",
        "epsilon",
        "gama",
        "zeta",
    };
    
    int
    main(int argc, char *argv[])
    {
        char **t;
        char s[] = "delta";
    
        t = bsearch(&s, tab, LEN(tab), sizeof *tab, (int(*)(const void *, const void *)) strcmp);
        if (t != NULL)
            printf("%s\n", *t);
    
        return EXIT_SUCCESS;
    }
    Last edited by felixthunder; 04-02-2020 at 10:05 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This works.
    Code:
    int
    main(int argc, char *argv[])
    {
        char **t;
        char *s = "delta";
    
        t = bsearch(&s, tab, LEN(tab), sizeof *tab, (int(*)(const void *, const void *)) strcmp);
        if (t != NULL)
            printf("%s\n", *t);
    
        return EXIT_SUCCESS;
    }
    s needs to be the same type as the things you're comparing against.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    10
    And what if I want to search for argv[1] instead of s. The following also does not work.

    Code:
    int
    main(int argc, char *argv[])
    {
        char **t;
    
        argc--;
        argv++;
        if (argc != 1)
            return EXIT_FAILURE;
    
        t = bsearch(argv, tab, LEN(tab), sizeof *tab, (int(*)(const void *, const void *)) strcmp);
        if (t != NULL)
            printf("%s\n", *t);
    
        return EXIT_SUCCESS;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You also can't use strcmp directly.
    The compar routine is expected to have two arguments which point to the key
    object and to an array member, in that order, and should return an integer less than, equal to, or greater than zero if the key object is found, respectively, to be less than, to match, or be greater
    than the array member.
    The compare function gets pointers to pointers.
    So it needs a simple wrapper to do one level of dereferencing to get to something strcmp can use.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define LEN(x) (sizeof (x) / sizeof (x[0]))
    
    char *tab[] = {
        "alfa",
        "beta",
        "delta",
        "epsilon",
        "gama",
        "zeta",
    };
    
    int mycmp(const void *a, const void *b) {
      const char * const *pa = a;
      const char * const *pb = b;
      return strcmp(*pa,*pb);
    }
    
    int
    main(int argc, char *argv[])
    {
        char **t;
    
        argc--;
        argv++;
        if (argc != 1)
            return EXIT_FAILURE;
    
        t = bsearch(argv, tab, LEN(tab), sizeof *tab, mycmp);
        if (t != NULL)
            printf("%s\n", *t);
    
        return EXIT_SUCCESS;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2020
    Posts
    10
    Quote Originally Posted by Salem View Post
    The compare function gets pointers to pointers.
    So it needs a simple wrapper to do one level of dereferencing to get to something strcmp can use.
    It worked, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bsearch in array of pointers to struct
    By redgr in forum C Programming
    Replies: 3
    Last Post: 03-19-2015, 08:36 AM
  2. Bsearch() isn't finding an integer key in my array.
    By MilleniumFalcon in forum C++ Programming
    Replies: 20
    Last Post: 03-22-2014, 04:26 PM
  3. Replies: 5
    Last Post: 10-13-2012, 11:43 PM
  4. Sorting Strings: Binary Search for string
    By alexhollis in forum C Programming
    Replies: 9
    Last Post: 03-15-2012, 12:30 PM
  5. search numbers in an inputed string [array of strings]
    By zattara13 in forum C Programming
    Replies: 1
    Last Post: 03-28-2008, 06:11 AM

Tags for this Thread