Thread: bsearch in array of pointers to struct

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    2

    bsearch in array of pointers to struct

    Hello to all, I'm new to the forum and I have a question,
    what I'm trying to do with this code is an address book and I have an array of pointers which are returned by malloc whenever I need to add an extra entry in the address book. What I need to do is search for a specific entry using bsearch. I've got an inqSort() function that sorts the table and runs normally, but my program crashes when I try to use bsearch. I can't understand what I'm doing wrong, so I was wondering if somebody could explain it to me

    Code:
    typedef struct {
        char name[20];
        char phone[14];
    } abEntry;
    
    abEntry * ab[100];
    
    int abFreeEntry=0;
    
    int abComp(const void *a, const void *b){
        const abEntry * ab1 = *(abEntry **) a;
        const abEntry * ab2 = *(abEntry **) b;
        return strcasecmp(ab1->name,ab2->name);
    }
    
    void inqSort(){
        qsort(ab,abFreeEntry,sizeof(abEntry *),abComp);
    }
    
    abEntry* findAbEntry(abEntry abe){
        return bsearch(&abe,*ab,abFreeEntry,sizeof(abEntry *),abComp);
    }
    
    void findEntryUI(){
        abEntry query, *res;
        
        printf("Input name to be searched: ");
        scanf("%s",&query.name);
    
        res=findAbEntry(query);
        if (res!=NULL){
            printf("The phone number is: %s\n",res->phone);
        } else {
            printf("\n***Entry not found***\n");
        }
    }
    Every time an entry is inserted, I inqsort() the array so it's always sorted, and it works as expected. But when I try to call findEntryUI(); from the main() function, the program crashes after entering the name I want to search. Any help is welcome! Thank you in advance!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > return bsearch(&abe,*ab,abFreeEntry,sizeof(abEntry *),abComp);
    Perhaps you meant ab, not *ab

    See your qsort() call.
    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 2015
    Posts
    2
    That was what I first tried, but when run the program crashes:

    Process exited after 5.43 seconds with return value 3221225477

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    I guess you're just going to have to post a complete program (with the input data) to demonstrate the problem.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. Array of pointers to a struct
    By Magical in forum C Programming
    Replies: 11
    Last Post: 03-20-2011, 06:03 PM
  3. array of pointers in struct
    By cs32 in forum C Programming
    Replies: 9
    Last Post: 04-09-2008, 01:43 PM
  4. bsearch and struct **pointers
    By reis_sg in forum C Programming
    Replies: 8
    Last Post: 05-25-2005, 11:05 AM
  5. array of struct pointers
    By jellyKing in forum C Programming
    Replies: 1
    Last Post: 04-12-2003, 12:17 PM