qsort( ) on a structured array

This is a discussion on qsort( ) on a structured array within the C Programming forums, part of the General Programming Boards category; I've searched online on the qsort( ) function and on this forum but cannot seem to find a lot of ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    20

    qsort( ) on a structured array

    I've searched online on the qsort( ) function and on this forum but cannot seem to find a lot of information on how to sort an array of structs. I seemed to get this code to compile but when I go to print out the array, it seems that the entire array is null. I've initialized the array already but didn't think anyone would need that. Here's only the code I'm having a problem with:

    PHP Code:
    /* an entry in Operation Table */
    struct ot_entry {
         
    char  ot_mnemonic[OP_LENGTH 1];
         
    int   ot_format;
    };

    /* array of entries */
    struct  ot_entry  *optbl[OT_SIZE];

    int compare_elements(const void *ptr1, const void *ptr2){    
        return 
    strcmp(((struct ot_entry*)ptr1)->ot_mnemonic, ((struct ot_entry*)ptr2)->ot_mnemonic);
    }    

    // sort Operation Table
    void sortOpTbl(optbl){
        
    qsort(optblsizeof(struct ot_entry), ot_countcompare_elements);
    }

    int main(){
        
        
    sortOpTbl(optbl);
        
        return 
    0;   

    Thanks in advance if anyone can help me figure out what I'm doing wrong..

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>/* array of entries */
    >>struct ot_entry *optbl[OT_SIZE];
    This is not an array of structs, its an array of pointers to structs.

    I assume you're actually assigning a value to these pointers? If not they'll most likely all be null (I'm assuming this array is a global variable?

    The sortOpTbl should be like so:
    void sortOpTbl(struct ot_entry optbl[])

    The call to qsort is also incorrect.

    Here is a sample, you code amended to run:
    (I change the comparison to compare the ints rather than the char arrays, just to save time).
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define OP_LENGTH 10
    #define OT_SIZE   10
    
    /* an entry in Operation Table */
    struct ot_entry
    {
      char  ot_mnemonic[OP_LENGTH + 1];
      int   ot_format;
    };
    
    /* array of entries */
    struct ot_entry optbl[OT_SIZE];
    
    int compare_elements(const void *ptr1, const void *ptr2)
    {
      return(((struct ot_entry *) ptr1)->ot_format - ((struct ot_entry *) ptr2)->ot_format);
    }
    
    // sort Operation Table
    void sortOpTbl(struct ot_entry optbl[])
    {
      qsort(optbl, OT_SIZE, sizeof(struct ot_entry), compare_elements);
    }
    
    int main(void)
    {
      int i;
      
      for (i = 0; i < OT_SIZE; i++)
      {
        optbl[i].ot_format = 10 - i;
        printf ("%d ", optbl[i].ot_format);
      }
      
      sortOpTbl(optbl);
      
      printf ("\nAfter:\n");
      
      for (i = 0; i < OT_SIZE; i++)
      {
        printf ("%d ", optbl[i].ot_format);
      }
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    Thanks a lot Hammer! I corrected my code and it works! Much appreciation...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 08:01 PM
  3. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 01:20 PM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-12-2004, 12:15 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21