Thread: stdlib qsort & bsearch question, comparing custom defined structure arrays

  1. #1
    Registered User daluu's Avatar
    Join Date
    Dec 2002
    Posts
    42

    stdlib qsort & bsearch question, comparing custom defined structure arrays

    I'm wondering if I can use the stdlib quicksort & binary search functions to search & sort a custom defined structure array containing a character string key. The functions would compare the key of 2 elements & use the result to search/sort structure array. Can it be done? if so, how do I modify the compare function below to make it work? Or will I have to write custom binary search & sorting functions for this situation?

    //code below
    typedef struct index{ // 1 entry in index contains these 3 fields
    char key[7];
    int rrn;
    int block;
    }Index;

    int compare(const void* p1, const void* p2);

    int main(){

    Index the_index[3];

    the_index[0].key[0] = 0;
    the_index[1].key[0] = 0;
    the_index[2].key[0] = 0;

    strcat(the_index[0].key,"N87BC ");
    strcat(the_index[1].key,"TC-THG");
    strcat(the_index[2].key,"N1079U");

    puts("Before sort:\n");
    int i;
    for(i = 0; i < 3; i++)
    printf("%s\t",the_index[i].key);
    puts(" ");

    qsort((void *)the_index, 3, (2*sizeof(int)+7*sizeof(char)), compare);

    puts("After sort:\n");
    int i;
    for(i = 0; i < 3; i++)
    printf("%s\t",the_index[i].key);
    puts(" ");
    return 0;
    }

    int compare(const void* p1, const void* p2){
    return strcmp(*(Index *).(char *)p1.key, *(Index *).(char *)p2.key);
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    //code below
    typedef struct  index
    { // 1 entry in index contains these 3 fields
      char  key[7];
      int   rrn;
      int   block;
    } Index;
    
    int compare(const void *p1, const void *p2);
    
    int main(void)
    {
      int i;
      Index the_index[3];
    
      the_index[0].key[0] = 0;
      the_index[1].key[0] = 0;
      the_index[2].key[0] = 0;
    
      strcat(the_index[0].key, "N87BC ");
      strcat(the_index[1].key, "TC-THG");
      strcat(the_index[2].key, "N1079U");
    
      puts("Before sort:\n");
    
      
      for (i = 0; i < 3; i++) printf("%s\t", the_index[i].key);
      puts(" ");
    
      qsort((void *) the_index, sizeof(the_index)/sizeof(the_index[0]), sizeof(Index), compare);
    
      puts("After sort:\n");
    
      for (i = 0; i < 3; i++) printf("%s\t", the_index[i].key);
      puts(" ");
      return(0);
    }
    
    int compare(const void *p1, const void *p2)
    {
      return strcmp(((Index*)p1)->key, ((Index*)p2)->key);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User daluu's Avatar
    Join Date
    Dec 2002
    Posts
    42
    thanks for the code suggestion! I figured I had to use the "this" pointer but wasn't sure how to implement it in.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by daluu
    thanks for the code suggestion! I figured I had to use the "this" pointer but wasn't sure how to implement it in.
    The "this" pointer is used in C++, not C, or are we getting things confused here?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User daluu's Avatar
    Join Date
    Dec 2002
    Posts
    42
    Hammer,

    well, I didn't say I was using strictly C but I'm trying to. And what would you call "->" in C then? As that is the code suggestion you gave me for the compare function.

    Salem,

    I don't know too much about qsort() & bsearch() in stdlib, I was just following what my textbook showed.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    "->" is a structure de-reference operator.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User daluu's Avatar
    Join Date
    Dec 2002
    Posts
    42
    I noticed that qsort code listed above doesn't work if you pass in a dynamic structure array initialized by new or calloc.

    how should the call to qsort & the compare function be rewritten then?

  8. #8
    Registered User daluu's Avatar
    Join Date
    Dec 2002
    Posts
    42
    thanks for the advice Salem.

    I knew something needed to be rewritten but I didn't know how or what - compare function OR the parameters passed into call to qsort.

    So for the compare function, I would compare pa vs. pb then right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing Arrays question
    By taugust7 in forum C++ Programming
    Replies: 36
    Last Post: 04-14-2009, 12:29 PM
  2. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 07:16 AM
  5. Header files
    By borland_man in forum C++ Programming
    Replies: 14
    Last Post: 02-22-2002, 04:30 AM